【问题标题】:Scripted drive info to text file works in win7 and server2008 but not working in XP文本文件的脚本驱动器信息在 win7 和 server2008 中有效,但在 XP 中无效
【发布时间】:2012-02-29 15:35:01
【问题描述】:

我一直在编写一个小命令脚本,它将在文本文件中为我提供运行它的任何 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 

【问题讨论】:

  • 您能否编辑您的问题以添加有关您所面临问题的更多详细信息?您看到的具体问题是什么?

标签: command-line batch-file cmd


【解决方案1】:
  • fsutil 需要 \ 附加到我的 XP 机器上的驱动器号。
  • 我不确定驱动类型格式是否因版本而异,但在 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

【讨论】:

  • 太棒了。它在 XP、Vista、Win7 和 Server 2008 R2 中完美运行。我非常感谢重定向的变化。我没有想到这一点。非常感谢您的帮助!
【解决方案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 下应该可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    相关资源
    最近更新 更多