【问题标题】:Detecting Windows Drive via VBScript通过 VBScript 检测 Windows 驱动器
【发布时间】:2011-08-07 19:26:03
【问题描述】:

我有一个检测本地硬盘驱动器号的 VBScript,它会将它们存储在某个位置。现在我想从中删除 Windows 驱动器。我的意思是首先它找到所有本地硬盘驱动器,然后检测 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

谢谢,

【问题讨论】:

    标签: vbscript drives


    【解决方案1】:

    我更喜欢

    1. 使用 FSO 代替 WMI
    2. 将驱动器放在可用集合中而不是字符串中
    3. 不将系统驱动器放入集合中而不是删除 以后再说

    所以:

      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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 2012-01-02
      • 2015-11-21
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多