KevinTanzer
Mon Mar 14 08:25:02 CST 2005
Thanks very much! Besides a being a spot-on, quick answer, I learned
something new from your response that helps me in my work.
"Torgeir Bakken (MVP)" wrote:
> Kevin Tanzer wrote:
>
> > How can I tell if a drive letter refers to a USB drive?
> > I need to differentiate between the kinds of removable
> > drives (USB memory stick, PCMCIA card, etc.).
> Hi
>
> '--------------------8<----------------------
>
> ' Obtain a dictionary object containing USB drive letters
> Set dicUSBDrives = GetUSBDrives
>
> ' Drive letter to test for
> strDrive = "e:"
>
> If dicUSBDrives.Exists(strDrive) Then
> WScript.Echo strDrive & " is an USB drive"
> Else
> WScript.Echo strDrive & " is not an USB drive"
> End If
>
>
> If dicUSBDrives.Count = 0 Then
> WScript.Echo "No USB drives found"
> Else
> WScript.Echo "The following USB drives was found:"
> For Each strUSBDrive In dicUSBDrives
> WScript.Echo strUSBDrive
> Next
> End If
>
>
> Function GetUSBDrives
> ' Populate a dictionary object with USB drive letters
> Set dicUSBList = CreateObject("Scripting.Dictionary")
> dicUSBList.CompareMode = vbTextCompare
>
> strComputer = "." ' use "." for local computer
> Set objWMI = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer _
> & "\root\cimv2")
> Set colDiskDrives = objWMI.ExecQuery _
> ("Select DeviceID from Win32_DiskDrive WHERE InterfaceType='USB'")
>
> For Each objDiskDrive In colDiskDrives
> strDeviceID = objDiskDrive.DeviceID
> strEscapedDeviceID = Replace(strDeviceID, "\", "\\")
>
> Set colDiskPartitions = objWMI.ExecQuery _
> ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" _
> & strEscapedDeviceID & """} WHERE " _
> & "AssocClass = Win32_DiskDriveToDiskPartition")
>
> For Each objDiskPartition In colDiskPartitions
>
> Set colLogicalDisks = objWMI.ExecQuery _
> ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
> objDiskPartition.DeviceID & """} WHERE " & _
> "AssocClass = Win32_LogicalDiskToPartition")
>
> For Each objLogicalDisk In colLogicalDisks
> dicUSBList.Add objLogicalDisk.DeviceID, ""
> Next
> Next
> Next
> Set GetUSBDrives = dicUSBList
> End Function
> '--------------------8<----------------------
>
>
>
> --
> torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and an ONLINE version of
> the 1328 page Scripting Guide:
>
http://www.microsoft.com/technet/scriptcenter/default.mspx
>