Batch Question: FOR


OSNN Senior Addict
10 Feb 2003
I'm trying to figure out the following. In the end I want the script to be detecting all mounted drives and running designated programs on them.
IF "HKLM\SYSTEM\MountedDevices" /v "\DosDevices\C:"=="5c ?" set Cdrive=0
Where "5c ?" is a hex value and 5c is the first term, if that term is 5c then the drive is not mounted. If another term is there, then the drive is mounted. I figure if I query each possible drive from C-Z in this way I can then do something like:
if "%Cdrive%"=="1" run program C: /switch
And so on down through to Z.

Mounted Drive info is stored in "HKLM\SYSTEM\MountedDevices".

Anyone have suggestions on a better way I'd love to hear it.

I've come up with an alternative, instead of using the registry entry as a flag, I'll use the "Recycler" folder created on all drives.
I needed it for my Maintenance Routine Script, and since I don't know any programming languages I had to do it all from DOS.

Specifically for the programs that didn't run on all drives automatically, I had to get them to detect the mounted drives then run the specified /switches on that drive.

For example:
IF EXIST C:\Recycler dirms c compact defrag
IF EXIST D:\Recycler dirms d compact defrag
IF EXIST E:\Recycler dirms e compact defrag

Right down to Z:\ maybe not the most efficient way ever but it works!

Luckily for me the Recycler folder exists on all mounted drives, or else I've have to figure something else out.

Maintenance Routine Thread

I'm still having trouble getting Regcleaner and Ad-ware to detect their installation locations, since they don't store their locations in the registry as a value. I've not figured that out yet.
You are aware that this "Recycler" trick won't work on FAT32 volumes? The recycle bin folder is named "Recycled" there.

To parse the registry value for mounted devices you could use this:

reg query HKLM\system\mounteddevices | find "DosDevices" | find /v /i "5C"  > :DRIVES
for /f "tokens=2 delims=\: " %%a in (:DRIVES) do echo %%a:\ >> test.txt

Replace echo %%a:\ >> test.txt with the command you want to use. The code above passes just the driveletter. So add ":" or ":\" to %%a just as needed.

Adaware writes its location into the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall.
Problem the name of the subkey is different depending on the version (Ad-aware 6 Personal or Ad-aware 6 Professional - are there others?)
You could use these two commands - one will fail:

for /f "usebackq skip=4 tokens=3" %%i in (`reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Ad-aware 6 Personal" /v UninstallString`) do start %%~dpi\Ad-aware.exe
for /f "usebackq skip=4 tokens=3" %%i in (`reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\Ad-aware 6 Professional" /v UninstallString`) do start %%~dpi\Ad-aware.exe


I never use any kind of reg cleaners. So don't know about that.
Thats pretty sweet info there, very helpful.

I get everything except the use of "usebackq skip=4 tokens=3" after reading tonnes of documentation this is the only thing that I just don't understand.
First an update for the Adaware batch without failing command

for /f "usebackq delims=" %%a in (`reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall ^| find "Ad-aware"`) do for /f "usebackq skip=4 tokens=3" %%i in (`reg query "%%a" /v UninstallString`) do start %%~dpiAd-aware.exe

Command line options for adaware:

Some links for understanding the for /f stuff. Maybe you know them already. It is mainly a process of trial and error and debugging to get these commands to work. Sometimes it is just the order the delimiters are listed that causes a batch to fail.

and of course for /? at the command prompt.


Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Latest member