Writing a .bat file to install, execute, and uninstall antimalware/virus software

JM Aggie08

Diamond Member
Jan 3, 2006
8,364
960
136
I've written a .bat file to install mbam, spybot and mse from my flash drive. In addition, it updates, runs and uninstalls (-mse) the programs.

My only issue is that the drive letter will change varying from machine to machine.

How can I write it to point at my flash drive if the drive letter is going to be different on most machines?


For example, on my machine it is H: \, and on a buddy's, it's F: \

I hope i've given enough enough to get a solution.
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
If the .bat file is giong to be stored on the flash drive and run from there, you can just capture the .bat file from the %0 argument which is the path and filename of the script.

Code:
REM Capture the drive letter from the %0 variable
set my_drive=%~d0
REM Change the drive letter
%my_drive%

If there are some dependencies you have to validate or things you need to find, you can search for them and once you find them you can capture the output from the CD command which will display the current path.

Code:
REM For loop to parse the output of cd
FOR /F "tokens=1-4 delims=*" %%A IN ('cd') DO SET my_drive=%%A
 

JM Aggie08

Diamond Member
Jan 3, 2006
8,364
960
136
I've never heard of that command before, but thanks!

Would that be put directly into the beginning of the code as written?
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
The code as written should be syntactically correct. It may not do everything you need, but it should get you started.

Where you put it in your code is pretty much up to you depending on your coding/formatting style and how you will use it. Batch scripts have a very limited use of scope in that most variables just end up being global, so you can access them from any point in the script.