How to slice the output of: FSUTIL FSINFO DRIVES

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
budhax
Posts: 63
Joined: 09 Oct 2006 12:25

How to slice the output of: FSUTIL FSINFO DRIVES

#1 Post by budhax » 01 Mar 2008 07:58

Hello,
this command: FSUTIL FSINFO DRIVES
outputs something like:

Code: Select all

Drives: A:\ C:\ D:\


1. How to get drives list, one by line?

Code: Select all

A:\
C:\
D:\


2. How to get "Drives: A:\ C:\ D:\" in a variable's value?
Thank in advance.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 01 Mar 2008 22:49

budhax,

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:
C:\>ver

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
Drives: C:\

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
C:\>type 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
Drives: C:\

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.
C:\>debug a.txt
-d100
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...

AHA :!: 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 ""

Drives: C:\
D:\
E:\

Yes :idea: 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!)"
C:\
D:\
E:\

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

set "drlist="
for /f "tokens=*" %%a in ('fsutil fsinfo drives^|find /v ""') do (
    set "dr=%%a"
    call set drlist=%%drlist%% %%dr:~-3%%
)
for %%a in (%drlist%) do echo %%a


DOS IT HELP? :wink:

Post Reply