通过 VBScript 检测 USB 和软盘驱动器的字母 [英] Detecting USB and Floppy Drives' Letters via VBScript

查看:23
本文介绍了通过 VBScript 检测 USB 和软盘驱动器的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Vb 脚本,它将所有可移动驱动器的字母存储到一个变量中,如您所知,它包含软盘和 USB 驱动器,我想将它们分开,我的意思是我想将 USB 驱动器的字母存储在一个变量中并将软盘放入另一个变量中,

I have a Vb-script which store all removable drives' letters into a variable, As you know it contains both floppy and USB drives, I want to seperate them, I mean I want to store USB Drives' Letters in a variable and Floppy ones into another variable,

脚本如下:

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

I'm using a software which can call VBScript. But it only support some kind of them like which I posted. So How can I do what I told?

提前致谢.

推荐答案

检查 objDisk.MediaType.此处您会找到一个列表媒体类型;乍一看 MediaType 1 ... 10 表示正常"软盘;在我的(虚拟)机器上快速检查时,USB 驱动器显示的 MediaType 为 Null(对于未知甚至不为零),因此您必须小心.再看一眼(谈论小心):大多数定义的媒体类型都识别软盘(其中一些是异国情调的).顺便说一句 - USB 软盘驱动器呢?

Check objDisk.MediaType. Here you'll find a list of MediaTypes; at a first glance MediaType 1 ... 10 indicates a 'normal' floppy; in a quick check on my (virtual) machine, an USB drive showed a MediaType of Null (not even Zero for Unknown), so you'll have do be careful. At a second glance (talking about carefull): most defined media types identify floppies (some of them exotic). BTW - what about USB floppy drives?

由于我无法在真实"计算机上进行测试,因此您必须仔细检查以下代码:

As I can't test on a 'real' computer, you'll have to double check the following code:

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 检查保持在第一个位置即可.

The null MediaType of my USBDrive may be a freakish accident. I tried to make tinkering with the evaluating of the MediaType easy by using a "Select Case True" control structure. VBScript will test the conditions of the Cases until the first true one, execute the corresponding statement(s), and 'break' to the End Select. So adding special cases and/or reordering cases is straightforward - just keep the IsNull check at first position.

这篇关于通过 VBScript 检测 USB 和软盘驱动器的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆