2011-08-11 88 views
1

我有一個Vb腳本,它將所有可移動驅動器的字母存儲到一個變量中,正如您所知它包含軟盤驅動器和USB驅動器,我想分離它們,我的意思是我想要存儲USB驅動器中的一個變量信件和軟盤的人到另一個變量,通過VBScript檢測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。但它只支持我發佈的某種類型的內容。所以我怎麼做我說的?

在此先感謝。

回答

2

檢查objDisk.MediaType。 Here你會找到一個MediaTypes列表;乍一看MediaType 1 ... 10表示'正常'軟盤;在我的(虛擬)機器上進行快速檢查時,USB驅動器顯示MediaType爲Null(甚至不知道爲零),因此您必須小心。乍看之下(談論carefull):大多數確定的媒體類型識別軟盤(其中一些是異國情調)。順便說一句 - 關於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將測試案例的條件,直到第一個真實案例的條件,執行相應的語句,然後'終止'到結束選擇。因此,添加特殊情況和/或重新排序情況非常簡單 - 只需將IsNull檢查保留在第一個位置即可。

+0

嗨Ekkehard.Horner,幹得好的人:) Huuuuuuuge謝謝。它工作正常。你很棒:)你能給我你的郵件嗎? – Nofuzy

2

您也可以嘗試此查詢

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

詳情檢查this鏈接。祝你好運

相關問題