我有一个Vb脚本,它将所有可移动驱动器的字母存储到一个变量中,你知道它包含软盘和USB驱动器,我想分开它们,我的意思是我想将USB驱动器的字母存储在一个变量中和软盘转换成另一个变量,
这是脚本:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
Removable = ""
For Each objDisk in colDisks
if objDisk.DriveType = 2 then
if Removable > "" then
Removable = Removable & ";"
end if
Removable = Removable & objDisk.DeviceID & "\"
end if
Next
我正在使用可以调用VBScript的软件。但它只支持我发布的某种类型。那我怎么做我说的呢?
先谢谢。
答案 0 :(得分:2)
检查objDisk.MediaType。 Here你会找到一个MediaTypes列表;乍一看MediaType 1 ... 10表示“正常”软盘;在快速检查我的(虚拟)机器时,USB驱动器显示MediaType为Null(甚至不为零的未知),所以你要小心。乍一看(谈论细心):大多数定义的媒体类型识别软盘(其中一些是异国情调)。 BTW - USB软盘驱动器怎么样?
由于我无法在“真实”计算机上进行测试,因此您必须仔细检查以下代码:
Const cnRemovableDisk = 2
Const cnMTypeUnknown = 0
Const cnMTypeNoFloppy = 11
Const cnMTypeFixedHD = 12
Dim strComputer : strComputer = "."
Dim objWMIService : Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Dim colDisks : Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
Dim Removable : Removable = ""
Dim Floppy : Floppy = ""
Dim USBDrive : USBDrive = ""
Dim objDisk
For Each objDisk in colDisks
If objDisk.DriveType = cnRemovableDisk Then
Removable = Removable & ";" & objDisk.DeviceID & "\"
Select Case True
Case IsNull( objDisk.MediaType )
WScript.Echo objDisk.DeviceID, "has MediaType null - assuming USB Drive."
USBDrive = USBDrive & ";" & objDisk.DeviceID & "\"
Case objDisk.MediaType = cnMTypeNoFloppy
WScript.Echo objDisk.DeviceID, "has MediaType 11 - assuming USB Drive."
USBDrive = USBDrive & ";" & objDisk.DeviceID & "\"
Case objDisk.MediaType = cnMTypeUnknown
WScript.Echo objDisk.DeviceID, "has MediaType 0 - assuming USB Drive."
USBDrive = USBDrive & ";" & objDisk.DeviceID & "\"
Case objDisk.MediaType = cnMTypeFixedHD
WScript.Echo objDisk.DeviceID, "has MediaType 12 - how can this happen?"
Case Else
WScript.Echo objDisk.DeviceID, "has MediaType", objDisk.MediaType, " - surely some kind of floppy."
Floppy = Floppy & ";" & objDisk.DeviceID & "\"
End Select
End If
Next
Removable = Mid( Removable, 2 )
Floppy = Mid( Floppy , 2 )
USBDrive = Mid( USBDrive , 2 )
WScript.Echo "Removable:", Removable
WScript.Echo "Floppy: ", Floppy
WScript.Echo "USBDrive: ", USBDrive
我的输出是:
A: has MediaType 5 - surely some kind of floppy.
F: has MediaType null - assuming USB Drive.
Removable: A:\;F:\
Floppy: A:\
USBDrive: F:\
我的USBDrive的null MediaType可能是一个奇怪的事故。我试图通过使用“Select Case True ”控制结构来修改MediaType的评估。 VBScript将测试案例的条件,直到第一个真实的条件,执行相应的语句,并“中断”到结束选择。因此,添加特殊情况和/或重新排序案例非常简单 - 只需将IsNull检查保留在第一个位置。
答案 1 :(得分:2)
您也可以尝试此查询
set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=2")
有关详细信息,请查看this链接。祝你好运