Batch Question: FOR

Discussion in 'Web Design & Coding' started by chaos945, Apr 12, 2004.

  1. chaos945

    chaos945 Moderator

    Messages:
    934
    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.
    Code:
    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:
    Code:
    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.

    /edit
    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.
     
  2. ignipotentis

    ignipotentis OSNN Addict

    Messages:
    127
    Sounds like you have your own question answered. The biggest question is why?
     
  3. chaos945

    chaos945 Moderator

    Messages:
    934
    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:
    Code:
    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

    /edit
    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.
     
  4. yoyo

    yoyo _________________

    Messages:
    1,557
    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:

    Code:
    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:

    Code:
    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.
     
  5. chaos945

    chaos945 Moderator

    Messages:
    934
    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.
     
  6. yoyo

    yoyo _________________

    Messages:
    1,557
    First an update for the Adaware batch without failing command

    Code:
    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:
    http://www.lavasoftsupport.com/index.php?showtopic=22411
    http://www.lavasoftsupport.com/index.php?act=ST&f=14&t=4041


    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.

    http://www.robvanderwoude.com/index.html
    http://www.robvanderwoude.com/ntfortokens.html

    http://www.ss64.com/nt/for.html

    and of course for /? at the command prompt.


    .
     
    chaos945 likes this.