2011-08-06 78 views
2

我有一個檢測本地硬盤驅動器字母的VBScript,它會將它們存儲在某個位置。現在我想從中刪除Windows驅動器。我的意思是首先找到所有本地硬盤,然後檢測Windows驅動器並將其從本地硬盤列表中刪除,然後將它們存儲在目標變量中。怎麼做?通過VBScript檢測Windows驅動器

這裏是VBScript中:

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set colDisks = objWMIService.ExecQuery _ 
    ("Select * from Win32_LogicalDisk") 

drives = "" 
For Each objDisk in colDisks 
    if objDisk.DriveType = 3 then 
    if drives > "" then 
     drives = drives & ";" 
    end if 
    drives = drives & objDisk.DeviceID & "\" 
    end if 
Next 

感謝,

回答

4

我寧願

  1. 使用FSO而不是WMI
  2. 具有驅動器中可用的集合,而不是一個字符串
  3. 不把系統驅動器放入集合而不是rem oving 後來

所以:

Dim goFS  : Set goFS  = CreateObject("Scripting.FileSystemObject") 
    Dim dicDTypes : Set dicDTypes = buildDicMKV(_ 
    vbTextCompare, Split("0 1 2 3 4 5"), Split("Unknown Removable Fixed Network CD-ROM RAM-Disk") _ 
) 
    Dim dicDrives : Set dicDrives = CreateObject("Scripting.Dictionary") 
    Dim oWSH  : Set oWSH  = CreateObject("WScript.Shell") 
    Dim sSysDir : sSysDir  = oWSH.Environment("PROCESS")("SYSTEMROOT") 
    WScript.Echo "sSysDir", sSysDir 
    Dim sSysDrive : sSysDrive  = goFS.GetDriveName(sSysDir) 
    WScript.Echo "sSysDrive", sSysDrive 
    Dim sSDLetter : sSDLetter  = Left(sSysDrive, 1) 
    WScript.Echo "sSDLetter", sSDLetter 
    Dim oDrive 
    For Each oDrive In goFS.Drives 
     WScript.Echo oDrive.DriveLetter, oDrive.DriveType, dicDTypes(CStr(oDrive.DriveType)) 
     If  "Fixed" = dicDTypes(CStr(oDrive.DriveType)) _ 
     And sSDLetter <> oDrive.DriveLetter Then 
     Set dicDrives(oDrive.DriveLetter) = oDrive 
     End If 
    Next  
    WScript.Echo "------------------" 
    Dim sDrive 
    For Each sDrive In dicDrives.Keys 
     Set oDrive = dicDrives(sDrive) 
     WScript.Echo oDrive.DriveLetter, oDrive.DriveType, dicDTypes(CStr(oDrive.DriveType)) 
    Next  

Function buildDicMKV(vbCompMode, aKeys, aValues) 
    Set buildDicMKV = CreateObject("Scripting.Dictionary") 
' compare 
'  Optional. If provided, compare is a value representing the comparison mode. 
'  Acceptable values are 0 (Binary), 1 (Text), 2 (Database). Values greater than 
'  2 can be used to refer to comparisons using specific Locale IDs (LCID). 
    buildDicMKV.CompareMode = vbCompMode 
    Dim nIdx 
    For nIdx = 0 To UBound(aKeys) 
     buildDicMKV.Add aKeys(nIdx), aValues(nIdx) 
    Next  
End Function 

輸出:

sSysDir C:\WINDOWS 
sSysDrive C: 
sSDLetter C 
A 1 Removable 
C 2 Fixed 
E 3 Network 
M 3 Network 
X 2 Fixed 
------------------ 
X 2 Fixed 

新增:

我懷疑你不能做自己,但無論如何:

Dim sSysDrive : sSysDrive = CreateObject("Scripting.FileSystemObject") _ 
     .GetDriveName( _ 
      CreateObject("WScript.Shell").Environment("PROCESS")("SYSTEMROOT")) 
    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 drives : drives = "" 
    Dim objDisk 
    For Each objDisk in colDisks 
     If  objDisk.DriveType = 3 _ 
     And objDisk.DeviceID <> sSysDrive Then 
     If drives > "" Then 
      drives = drives & ";" 
     End If 
     drives = drives & objDisk.DeviceID & "\" 
     End if 
    Next 
    WScript.Echo drives 
+0

嗨Ekkehard.Horner,謝謝你的回覆。但我只需要編輯我的代碼。我無法使用其他代碼。我正在使用一個我可以稱之爲VBScript的軟件,就像我告訴過的那樣。那麼你能幫我編輯我的代碼嗎? – Nofuzy

+0

嗨Ekkehard.Horner,謝謝veryyyyyyyyyyyyyyyyyyyyyyyy很多。大。你添加的部分工作正常,所以這個問題是由你解決:)再次感謝... – Nofuzy