I\'m trying to write little program which will be able to read some informations about REMOVEABLE_DEVICE (USB). I\'ve tried pyusb but I was not able to pull data I need.
Disclaimer: I haven't really got any experience with the win32com.client
library.
By starting with Win32_DiskDrive
like you did, I went over Win32_DiskDriveToDiskPartition and Win32_LogicalDiskToPartition, and then to Win32_LogicalDisk to get the VolumeName
which seems what you want.
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
# 1. Win32_DiskDrive
colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\"")
DiskDrive_DeviceID = colItems[0].DeviceID.replace('\\', '').replace('.', '')
DiskDrive_Caption = colItems[0].Caption
print 'DiskDrive DeviceID:', DiskDrive_DeviceID
# 2. Win32_DiskDriveToDiskPartition
colItems = objSWbemServices.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition")
for objItem in colItems:
if DiskDrive_DeviceID in str(objItem.Antecedent):
DiskPartition_DeviceID = objItem.Dependent.split('=')[1].replace('"', '')
print 'DiskPartition DeviceID:', DiskPartition_DeviceID
# 3. Win32_LogicalDiskToPartition
colItems = objSWbemServices.ExecQuery("SELECT * from Win32_LogicalDiskToPartition")
for objItem in colItems:
if DiskPartition_DeviceID in str(objItem.Antecedent):
LogicalDisk_DeviceID = objItem.Dependent.split('=')[1].replace('"', '')
print 'LogicalDisk DeviceID:', LogicalDisk_DeviceID
# 4. Win32_LogicalDisk
colItems = objSWbemServices.ExecQuery("SELECT * from Win32_LogicalDisk WHERE DeviceID=\"" + LogicalDisk_DeviceID + "\"")
print 'LogicalDisk VolumeName:', colItems[0].VolumeName
print
# putting it together
print DiskDrive_Caption
print colItems[0].VolumeName, '(' + LogicalDisk_DeviceID + ')'
Works for me:
DiskDrive DeviceID: PHYSICALDRIVE1
DiskPartition DeviceID: Disk #1, Partition #0
LogicalDisk DeviceID: D:
LogicalDisk VolumeName: PENDRIVE
Sony Storage Media USB Device
PENDRIVE (D:)
This seems to provide a complicated, but possible way, maybe you can simplify it even more. My only simplification is leaving out Win32_DiskPartition already because we only need the connection.
Please note:
\\.\PHYSICALDRIVE1
, but it should be possible to get rid of the .replace()
-methods.JOIN
them SQL-like?).