问题
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,
Here is the script:
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
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?
Thanks in Advance.
回答1:
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
my output is:
A: has MediaType 5 - surely some kind of floppy.
F: has MediaType null - assuming USB Drive.
Removable: A:\;F:\
Floppy: A:\
USBDrive: F:\
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.
回答2:
You can also try this query
set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=2")
For further details check this link. Best of luck
来源:https://stackoverflow.com/questions/7022696/detecting-usb-and-floppy-drives-letters-via-vbscript