How to slice the output of: FSUTIL FSINFO DRIVES

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

How to slice the output of: FSUTIL FSINFO DRIVES

Post by MigrationUser »

01 Mar 2008 14:59
budhax

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

Drives: A:\ C:\ D:\

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

A:\
C:\
D:\

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

----------------------------

#2 02 Mar 2008 16:36
bluesxman

That was a little more challenging that I expected, due to some weirdness with the output produced by FSUTIL (contrary to how it might appear, those aren't spaces between the items in the list.) Nevertheless, I worked around it, this should do what you ask on both counts (tested on WinXP only):

Code: Select all

@echo off
setlocal enabledelayedexpansion
set drives=
for /f "usebackq tokens=1*" %%a in (`FSUTIL FSINFO DRIVES ^| find ":"`) do (
    if /i "%%a" NEQ "Drives:" (
        set "drives=!drives! %%a"
        echo:%%a
    ) ELSE (
        set "drives=!drives! %%b"
        echo:%%b
    )
)

set drives=%drives:~1%
echo:"%drives%"

pause
Last edited by bluesxman (02 Mar 2008 16:38)

cmd | *sh | ruby | chef

----------------------------

#3 23 May 2009 05:15
blufr0g

This works but then you end up with "Drives:" as one of the outputs, which doesn't really matter if you are passing the variable through some command it will just fail and move to the next actual drive:

Code: Select all

FOR /F "usebackq tokens=1" %%x IN (`FSUTIL FSINFO DRIVES ^| FIND ":"`) DO @ECHO %%x
This runs twice as fast as the fsutil approach and most importantly does not output "Drives:"

Code: Select all

FOR /F "usebackq tokens=1" %%x IN (`MOUNTVOL ^| FIND ":\"`) DO @ECHO %%x
Working examples, output removable drive letters:

Code: Select all

FOR /F "usebackq tokens=1" %%a IN (`FSUTIL FSINFO DRIVES ^| FIND ":"`) DO (
FOR /F "usebackq tokens=3" %%b IN (`FSUTIL FSINFO DRIVETYPE %%a`) DO (
IF /I "%%b" EQU "Removable" ECHO %%a ))
or better yet:

Code: Select all

FOR /F "usebackq tokens=1" %%a IN (`MOUNTVOL ^| FIND ":\"`) DO (
FOR /F "usebackq tokens=3" %%b IN (`FSUTIL FSINFO DRIVETYPE %%a`) DO (
IF /I "%%b" EQU "Removable" ECHO %%a ))
----------------------------

#4 24 May 2009 03:16
avery_larry

See this discussion:
http://ss64.org/oldforum/viewtopic.php?id=410

----------------------------
14 Dec 2009 11:01
bluesxman

For what it's worth, I have a script which makes a decent attempt at figuring out drive types (beyond what "fsutil" will show you). I think I posted it on the forum somewhere already, but here it is again anyway:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set drive=
echo:
for /f "usebackq tokens=1*" %%a in (`fsutil fsinfo drives ^| find ":"`) do (
    if /i "%%a" NEQ "Drives:" (
        set "drive=%%a"
    ) ELSE (
        set "drive=%%b"
    )
    set label=
    for /f "usebackq tokens=1* delims=\- " %%A in (`fsutil fsinfo drivetype !drive!`) do (
            for /f "usebackq tokens=5*" %%U in (`vol %%A 2^>nul ^| findstr "^.Volume.in.drive...is" ^| findstr /v "^.Volume.Serial"`) do set label=%%V
        set type=%%B

    )
    set "label=!label!                                "
    set type=!type:~0,-1!
    set drive=!drive:\=!
    for /f "usebackq tokens=2*" %%R in (`subst 2^>nul ^| findstr /i /b "!drive!"`) do set type=SUBST : %%S
    echo:!drive! [!label:~0,32!] !type!
)
echo:
pause
cmd | *sh | ruby | chef
----------------------------
25 Nov 2011 14:33
RG

I can confirm what some of you have suspected... FSUTIL output definitely varies by OS!. Thanks Microsoft!
I don't have multiple machines to test with at the moment, but comments in one of my files have this note about Vista (and presumably newer OS's)
"FSUTIL FSINFO DRIVES on Vista puts nulls between drives instead of spaces!"
Some of the the wierdness in FSUTIL's output (referred to by bluesxman) is vertical tabs and single linefeeds (no CR).
I think there are also trailing CR's.... maybe only on some OS's... which would explain one of the posts farther back where a trailing character was being removed.
The moral of the story here is....whatever you come up with using FSUTIL needs to be tested across all OS's that you might encounter!
There are a lot of posts here so I won't test each suggestion, but if you want to repost the current implementation and I can test on whatever OS you don't have available.

Windows Shell Scripting and InstallShield

----------------------------

#29 11 Aug 2017 18:01
Simon Sheppard

Resurrecting an ancient thread, this works on Windows 7+

Code: Select all

:: Store all the drive letters currently in use in a variable
For /f "tokens=*" %%L in ('FSUTIL fsinfo drives') do (set _drives=%%L)
 
:: Remove the first 8 characters - the 'Drives:' prefix
:: this may need to be adjusted for other languages/locales
Set _drives=%_drives:~8%
 
:: Find and Display the drive type of each drive
For %%D in (%_drives%) do (FSUTIL fsinfo drivetype %%D)
Post Reply