【问题标题】:Detecting USB and Floppy Drives' Letters via VBScript通过 VBScript 检测 USB 和软盘驱动器的字母
【发布时间】:2013-08-15 02:21:50
【问题描述】:

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

提前致谢。

【问题讨论】:

    标签: vbscript drives


    【解决方案1】:

    检查 objDisk.MediaType。 Here 你会找到一个 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 的空 MediaType 可能是一个奇怪的意外。我试图通过使用“Select Case True”控制结构来简化对 MediaType 的评估。 VBScript 将测试 Cases 的条件,直到第一个为真,执行相应的语句,并“中断”到 End Select。因此,添加特殊情况和/或重新排序情况很简单 - 只需将 IsNull 检查放在首位即可。

    【讨论】:

    • 嗨 Ekkehard.Horner,干得好人 :) Huuuuuuuuge 谢谢。它工作正常。你太棒了:)你能给我你的邮件吗?
    【解决方案2】:

    你也可以试试这个查询

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

    更多详情请查看this 链接。祝你好运

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-07
      • 2012-01-02
      • 1970-01-01
      • 2015-11-21
      • 2023-04-07
      • 2020-03-19
      • 1970-01-01
      • 2011-03-24
      相关资源
      最近更新 更多