Indeed the "fsutil fsinfo drives" output show some interesting behavior when trying to capture the output with the FOR command.
If you are only interested in the solution just skip to the bottom, or look here: http://www.dostips.com/DtCodeSnippets.php#_Toc141112837
Ok now - let's go to the bottom of this:
For later reference it's good to dump the OS version first:
Microsoft Windows XP [Version 5.1.2600]
Now what does "fsutil fsinfo drives" return on my box:
C:\>fsutil fsinfo drives
Drives: C:\ D:\ E:\
So far so good, let's try to capture the output with the FOR command:
C:\>for /f "tokens=*" %a in ('fsutil fsinfo drives') do @echo.%a
Wait a second, that's strange, what happened with D:\ and E:\?
We'll need to take a closer look at the output. Let's dump the output into a text file and inspect the text file.
C:\>fsutil fsinfo drives>a.txt
Drives: C:\ D:\ E:\
No obvious problem using the TYPE command, so let's capture the TYPE output with a FOR command.
C:\>for /f "tokens=*" %a in ('type a.txt') do @echo.%a
There it happens again, D:\ and E:\ are missing. The problem must have carried over into the a.txt file. Let's take a closer look at a.txt with a HEX editor. The DEBUG command that ships with XP will work just fine.
0B05:0100 0D 0A 44 72 69 76 65 73-3A 20 43 3A 5C 00 44 3A ..Drives: C:\.D:
0B05:0110 5C 00 45 3A 5C 00 0D 0A-CE 92 B4 60 34 00 F4 0A \.E:\......`4...
See the 00? You would expect 20 (space) here instead of 00 (string end). Clearly a defect in the FSUTIL command.
Well looks like some commands like TYPE don't care about string end characters in a string. Can we do some preprocessing before parsing with the FOR command? Let's try piping the FSUTIL output through the FIND command. FIND /v "" will find all lines that are not empty, so in a way it will preserve the FSUTIL output we are interested in and do its internal processing and hopefully get rid of the 00s.
C:\>fsutil fsinfo drives|find /v ""
The 00s are now line breaks, this helps. We just need to get rid of the "Drives: " string in the row for the first drive. This can be done by using only the least 3 characters of each row. From the command line the following worked for me.
C:\>cmd /v:ON /c "for /f "tokens=*" %a in ('fsutil fsinfo drives^|find /v ""') do @(set d=%a&echo !d:~-3!)"
For use in a batch file the following snippet creates a space seperated list of drive letters in the drlist variable and then dumps the drlist content one by one in a second FOR loop.
Code: Select all
for /f "tokens=*" %%a in ('fsutil fsinfo drives^|find /v ""') do (
call set drlist=%%drlist%% %%dr:~-3%%
for %%a in (%drlist%) do echo %%a
DOS IT HELP?