1

我有一个 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 的软件。但它只支持我发布的某些类型。那么我该怎么做我说的呢?

提前致谢。

4

2 回答 2

2

检查 objDisk.MediaType。在这里你会找到一个 MediaTypes 列表;乍一看,MediaType 1 ... 10 表示“普通”软盘;在对我的(虚拟)机器进行快速检查时,USB 驱动器显示的 MediaType 为 Null(对于未知,甚至为零),因此您必须小心。再看一眼(谈论谨慎):大多数定义的媒体类型都标识软盘(其中一些是异国情调的)。顺便说一句 - 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 将测试 Cases 的条件,直到第一个为真,执行相应的语句,并“中断”到 End Select。因此,添加特殊情况和/或重新排序情况很简单——只需将 IsNull 检查保持在第一位。

于 2011-08-11T08:48:32.057 回答
2

你也可以试试这个查询

set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=2")

有关更多详细信息,请查看链接。祝你好运

于 2013-08-14T18:21:50.797 回答