脚本化的驱动器信息到文本文件在win7和server2008中工作,但在XP中不起作用

时间:2012-02-29 15:35:01

标签: command-line batch-file cmd

我一直在研究一个小命令脚本,它将为我提供在文本文件中运行它的任何Windows系统的驱动器布局。它目前在win7和Server2008中工作,但它无法在WinXP中提供完整信息。我不知道为什么。

@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

编辑: Win7和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 

XP的输出:

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

C: Fixed  

D: CD-ROM  

E: CD-ROM  

F: Fixed  

N: Remote/Network

T: Remote/Network  

我现在正在获取XP中第一个固定驱动器的详细信息,但第二个错误输出。我在上面的脚本中添加了一行来回显它正在使用的命令,看起来是正确的。我可以将命令从文本文档复制/粘贴到命令窗口,它可以工作。但是,现在真正的问题是Win7 PC的响应。这是新的输出:

XP输出:

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输出:

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

C: Fixe 

D: Fixe 

E: CD-RO 

F: CD-RO 

G: CD-RO 

H: Fixe 

2 个答案:

答案 0 :(得分:4)

  • fsutil要求在我的XP计算机上附加\个驱动器号。
  • 我不确定不同版本之间的drivetype格式是否有所不同,但更简单的是在fsutil fsinfo drivetype输出中的任何地方寻找“固定”格式更安全。
  • fsutil输出在最后(至少在XP上)有一个额外的回车符,在某些情况下可能会干扰。可以使用call set
  • 删除它
  • 我不得不在\命令中添加额外的subst | findstr,因为现在包含\并且它是findstr转义字符。

其他小问题

  • 无需在顶部初始化驱动器
  • 每条输出线的末端都有一个额外的空间,通过将重定向移动到命令的前面可以轻松修复,或者更好......
  • 通过重定向带括号的命令块,可以更快地进行一次重定向。

以下适用于XP和Vista(我没有Windows 7进行测试)

@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

答案 1 :(得分:2)

您的问题是set type=!type:~0,-6!type的末尾留下了额外的空格。我不知道它为何在Windows 7 / Server 2008 R2下运行,但我猜他们改变了IF在使用EQU时检查相等性的方式。但是,在Windows XP下,"Fixed "不等于"Fixed"

如果您将行set type=!type:~0,-6!更改为set type=!type:~0,-7!,则应在XP下正常工作。