简体   繁体   中英

Scripted drive info to text file works in win7 and server2008 but not working in XP

I have been working on a little command script that will give me the drive layout of any windows system I run it on in a text file. It is currently working in win7 and Server2008 but it fails to give complete info in WinXP. I am not sure why.

@echo 
setlocal enabledelayedexpansion
set drive=

rem wscript //NoLogo //B %~dp0system_profile.vbs

set textfile=%~dp0system_profile.txt

echo Drive Information >> "%textfile%"
echo ----------------- >> "%textfile%"
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 drive=!drive:\=!
    for %%D in (!drive!) do (
        for /f "usebackq tokens=1* delims=\- " %%A in (`fsutil fsinfo drivetype %%D`) do (
            set type=%%B
        )
        set type=!type:~0,-6!
        for /f "usebackq tokens=2*" %%R in (`subst 2^>nul ^| findstr /i /b "%%D"`) do set type=SUBST : %%S

        echo: >> "%textfile%"
        echo:%%D !type! >> "%textfile%"

        if /i "!type!" EQU "Fixed" (
        echo:fsutil fsinfo volumeinfo %%D >> "%textfile%"
        fsutil fsinfo volumeinfo %%D >> "%textfile%"
        )
    )
)
pause

Edit: The output from Win7 and Win Server 2008 R2:

C: Fixed 
Volume Name : OS
Volume Serial Number : 0x4e24ac66
Max Component Length : 255
File System Name : NTFS
Supports Case-sensitive filenames
Preserves Case of filenames
Supports Unicode in filenames
Preserves & Enforces ACL's
Supports file-based Compression
Supports Disk Quotas
Supports Sparse files
Supports Reparse Points
Supports Object Identifiers
Supports Encrypted File System
Supports Named Streams
Supports Transactions
Supports Hard Links
Supports Extended Attributes
Supports Open By FileID
Supports USN Journal

D: Fixed 
Volume Name : New Volume
Volume Serial Number : 0x490067a
Max Component Length : 255
File System Name : NTFS
Supports Case-sensitive filenames
Preserves Case of filenames
Supports Unicode in filenames
Preserves & Enforces ACL's
Supports file-based Compression
Supports Disk Quotas
Supports Sparse files
Supports Reparse Points
Supports Object Identifiers
Supports Encrypted File System
Supports Named Streams
Supports Transactions
Supports Hard Links
Supports Extended Attributes
Supports Open By FileID
Supports USN Journal

E: CD-ROM 

F: CD-ROM 

G: CD-ROM 

The output from XP:

Drive Information 
----------------- 

C: Fixed  

D: CD-ROM  

E: CD-ROM  

F: Fixed  

N: Remote/Network

T: Remote/Network  

I am now getting details for the first fixed drive in XP, but the second one errors out. I added a line to the script above to echo out the command it is using and it appears correct. I can copy/paste the command from the text document into the command window and it works. But, the real issue now is the response from the Win7 PC. Here is the new output:

XP output:

C: Fixed 
fsutil fsinfo volumeinfo C: 
Volume Name : 
Volume Serial Number : 0xa4728da7
Max Component Length : 255
File System Name : NTFS
Supports Case-sensitive filenames
Preserves Case of filenames
Supports Unicode in filenames
Preserves & Enforces ACL's
Supports file-based Compression
Supports Disk Quotas
Supports Sparse files
Supports Reparse Points
Supports Object Identifiers
Supports Encrypted File System
Supports Named Streams

D: CD-ROM 

E: CD-ROM 

F: Fixed 
fsutil fsinfo volumeinfo F: 
Error:  The filename, directory name, or volume label syntax is incorrect.

Win7 output:

Drive Information 
----------------- 

C: Fixe 

D: Fixe 

E: CD-RO 

F: CD-RO 

G: CD-RO 

H: Fixe 
  • fsutil requires \\ appended to drive letter on my XP machine.
  • I'm not sure if drivetype formats vary between versions, but safer to simply look for "fixed" anywhere within fsutil fsinfo drivetype output.
  • fsutil output has an extra carriage return at the end (at least on XP) that may have been interfering in some cases. It can be removed by using call set .
  • I had to add an extra \\ to the subst | findstr subst | findstr command because the \\ is now included and it is the findstr escape character.

Additional minor points

  • No need to initialize drive at top
  • Each of your output lines had an extra space at the end, easily fixed by moving the redirection to the front of the command, or better yet...
  • It is faster to do redirection once by redirecting a parenthesized block of commands.

The following works for me on both XP and Vista (I don't have Windows 7 to test)

@echo off
setlocal enabledelayedexpansion

rem wscript //NoLogo //B %~dp0system_profile.vbs

set textfile="%~dp0system_profile.txt"

(
  echo Drive Information
  echo -----------------
  for /f "usebackq tokens=1*" %%a in (`fsutil fsinfo drives ^| find ":"`) do (
      if /i "%%a" NEQ "Drives:" (
          call set "drive=%%a"
      ) ELSE (
          call set "drive=%%b"
      )
      for %%D in (!drive!) do (
          for /f "usebackq delims=" %%A in (`fsutil fsinfo drivetype %%D`) do (
              call set type=%%A
          )
          for /f "usebackq tokens=2*" %%R in (`subst 2^>nul ^| findstr /i /b "%%D\"`) do set type=%%D - SUBST : %%S
          echo:
          echo:!type!
          if "!type:fixed=!" NEQ "!type!" (
              echo:fsutil fsinfo volumeinfo %%D
              fsutil fsinfo volumeinfo %%D
          )
      )
  )
) >> %textfile%
pause

Your problem is that set type=!type:~0,-6! leaves an extra space at the end of type . I don't why it works under Windows 7/Server 2008 R2, but I guess they changed how IF checks for equality when using EQU . However, under Windows XP, "Fixed " doesn't equal "Fixed" .

If you change the line set type=!type:~0,-6! to set type=!type:~0,-7! , it should work fine under XP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM