一个 Vbscript 问题重新文件系统检测输出到变量? (OS XPhome NTFS)
A Vbscript question re filesystem detection output to Variable? (OS XPHome NTFS)
我希望有 Vbscripting 经验的人能够用 Vbscript 帮助我(我的 OS 是带有 NTFS 文件系统的 XP Home,由于 [=30= 我不能在 Vbs 代码中使用 WMI ])
我查看了 w.w.w 并找到了两个 VBscript 片段,它们将 (1) 在 C:\ (%systemdrive%) 驱动器上找到当前文件系统,第二个代码片段将识别 OS fileSystem 但是我真的很想将代码的两部分结合起来 & (2) 输出最好是一个变量,而不是稍后在批处理文件中获取的 msgbox。代码片段如下。
set shell = createobject("wscript.shell")
set environ = shell.environment("process")
systemdrive = environ("systemdrive")
msgbox systemdrive
set fso = CreateObject("Scripting.FileSystemObject")
set drive = fso.GetDrive("C")
Wscript.Echo "FileSystem =", drive.FileSystem
第一组代码创建一个名为 systemdrive 的变量并输出到消息框。第二个(在消息框中)在我的例子中输出 "Filesystem" "NTFS".
我的问题是当我将系统驱动器信息交换到行中时
set drive = fso.GetDrive("C") - 像这样设置 drive = fso.GetDrive("systemdrive") 而不是 ("C")
我收到错误代码消息。此外,我正在寻找 %variable% 输出而不是消息框输出,例如"FAT32, NTFS or whatever"。我希望我想表达的意思有意义吗?本质上,我通常尝试使用 systemdrive 变量 "C:" 而不是硬编码的 c: 路径找到 OS 文件系统并仅将其输出到变量?
据我了解,您想检测驱动器的文件系统并将其存储在环境变量中以供以后在批处理文件中使用。
这意味着您计划从该批处理文件中调用 VBScript,并且可以使用该批处理文件解析任何环境变量,例如 %SYSTEMDRIVE%
,而无需从VBS 文件(这是可能的,但当我们可以将驱动器号作为参数传递给脚本时,它会更加灵活)。
让我们制作一个接受一个参数并查找文件系统类型(如果可以)的 VBS。
Option Explicit
Dim shell, fso, drive, driveletter
' we expect a single argument - a drive letter in the form X:
If WScript.Arguments.Unnamed.Count = 0 Then Die "Please specify drive letter."
driveletter = WScript.Arguments.Unnamed(0)
Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
' try to get the drive object for that drive letter, die on error
On Error Resume Next
Set drive = fso.GetDrive(driveletter)
If Err.Number <> 0 Then Die "Could not detect filesystem type for " + driveletter + " (" + Err.Description + ")"
Wscript.StdOut.Write drive.FileSystem
Sub Die(message)
WScript.StdErr.WriteLine message
WScript.Quit 1
End Sub
现在可以独立调用了(出于命令行目的,使用命令行脚本解释器,cscript.exe):
cscript /nologo filesystem.vbs %SYSTEMDRIVE%
或者可以从批处理文件中调用它(我们将使用 for
循环将脚本的输出分配给变量):
@echo off
for /f "usebackq delims=" %%s in (`cscript /nologo filesystem.vbs %SYSTEMDRIVE%`) do (
set FILESYSTEM=%%s
)
echo The filesystem in %SYSTEMDRIVE% is %FILESYSTEM%.
上面为我打印了这个:
The filesystem in C: is NTFS.
我希望有 Vbscripting 经验的人能够用 Vbscript 帮助我(我的 OS 是带有 NTFS 文件系统的 XP Home,由于 [=30= 我不能在 Vbs 代码中使用 WMI ])
我查看了 w.w.w 并找到了两个 VBscript 片段,它们将 (1) 在 C:\ (%systemdrive%) 驱动器上找到当前文件系统,第二个代码片段将识别 OS fileSystem 但是我真的很想将代码的两部分结合起来 & (2) 输出最好是一个变量,而不是稍后在批处理文件中获取的 msgbox。代码片段如下。
set shell = createobject("wscript.shell")
set environ = shell.environment("process")
systemdrive = environ("systemdrive")
msgbox systemdrive
set fso = CreateObject("Scripting.FileSystemObject")
set drive = fso.GetDrive("C")
Wscript.Echo "FileSystem =", drive.FileSystem
第一组代码创建一个名为 systemdrive 的变量并输出到消息框。第二个(在消息框中)在我的例子中输出 "Filesystem" "NTFS".
我的问题是当我将系统驱动器信息交换到行中时 set drive = fso.GetDrive("C") - 像这样设置 drive = fso.GetDrive("systemdrive") 而不是 ("C") 我收到错误代码消息。此外,我正在寻找 %variable% 输出而不是消息框输出,例如"FAT32, NTFS or whatever"。我希望我想表达的意思有意义吗?本质上,我通常尝试使用 systemdrive 变量 "C:" 而不是硬编码的 c: 路径找到 OS 文件系统并仅将其输出到变量?
据我了解,您想检测驱动器的文件系统并将其存储在环境变量中以供以后在批处理文件中使用。
这意味着您计划从该批处理文件中调用 VBScript,并且可以使用该批处理文件解析任何环境变量,例如 %SYSTEMDRIVE%
,而无需从VBS 文件(这是可能的,但当我们可以将驱动器号作为参数传递给脚本时,它会更加灵活)。
让我们制作一个接受一个参数并查找文件系统类型(如果可以)的 VBS。
Option Explicit
Dim shell, fso, drive, driveletter
' we expect a single argument - a drive letter in the form X:
If WScript.Arguments.Unnamed.Count = 0 Then Die "Please specify drive letter."
driveletter = WScript.Arguments.Unnamed(0)
Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
' try to get the drive object for that drive letter, die on error
On Error Resume Next
Set drive = fso.GetDrive(driveletter)
If Err.Number <> 0 Then Die "Could not detect filesystem type for " + driveletter + " (" + Err.Description + ")"
Wscript.StdOut.Write drive.FileSystem
Sub Die(message)
WScript.StdErr.WriteLine message
WScript.Quit 1
End Sub
现在可以独立调用了(出于命令行目的,使用命令行脚本解释器,cscript.exe):
cscript /nologo filesystem.vbs %SYSTEMDRIVE%
或者可以从批处理文件中调用它(我们将使用 for
循环将脚本的输出分配给变量):
@echo off
for /f "usebackq delims=" %%s in (`cscript /nologo filesystem.vbs %SYSTEMDRIVE%`) do (
set FILESYSTEM=%%s
)
echo The filesystem in %SYSTEMDRIVE% is %FILESYSTEM%.
上面为我打印了这个:
The filesystem in C: is NTFS.