Batch file loop help, extra iteration?

visionviper

[H]ard|Gawd
Joined
Jul 24, 2007
Messages
1,222
I'm trying to put a batch file together to run a program on each logical drive on a system except for optical drives. It filters out the optical drives just fine but for some reason my echo of the built up command I am testing with runs one extra time except the variables have no values. It's like the loop is iterating an extra time on a null value. Ideas?

(The IF NOT !drive! == %safe% is just making sure I don't run the command for the drive the script lives on and "safe" is just an acronym)
Code:
for /F "usebackq skip=1 tokens=1,2" %%a IN (`wmic logicaldisk get DeviceID^,DriveType`) do (
    SET drive=%%a
    SET driveType=%%b
    IF NOT !drive! == %safe% (
        IF NOT !driveType! == 5 (
        ECHO test commands
        )
    )
)
 
The wmic command output includes an extra CR at the end. On stackoverflow someone made a workaround by adding an additional for loop to give you something like this:

Code:
for /F "usebackq skip=1 tokens=1,2" %%a IN (`wmic logicaldisk get DeviceID^,DriveType`) do for /F "tokens=1 delims=:" %%c in ("%%a") do (
    SET drive=%%c
    SET driveType=%%b
    IF NOT !drive! == %safe% (
        IF NOT !driveType! == 5 (
        ECHO test commands
        )
    )
)
 
Works great, thanks! I've been trying to figure it our for hours today. Where did you happen to find that (one of the places I've been looking was stackoverflow)
 
Back
Top