Belini Posted August 3, 2014 Share Posted August 3, 2014 (edited) I'm using a code to return the serial number and the letter of a flash drive, works very well if the connected device is a USB stick gives more error if you have a USB plugged drawer on the computer, how can I do to avoid this error? #include <Array.au3> eachUsbDevice() Func getUsbSerial() Local $letter[1], $disk[1], $aArray[1][2] Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2") Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", 0x10 + 0x20) For $objitem In $colItems If $objitem.interfacetype = "USB" Then For $partition In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $objitem.DeviceId & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition") Next For $loDisk In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $partition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") Next $aRegExSer = StringRegExp($objitem.pnpdeviceid, "\x5C([0-9a-fxA-FX]+)&", 3) If IsArray($aRegExSer) Then _ArrayAdd($disk, $aRegExSer[0]) _ArrayAdd($letter, $loDisk.DeviceId) EndIf EndIf Next ReDim $aArray[UBound($disk)][2] For $i = 0 To UBound($disk) - 1 $aArray[$i][0] = $disk[$i] $aArray[$i][1] = $letter[$i] Next Return $aArray EndFunc ;==>getUsbSerial Func eachUsbDevice() Local $ret = getUsbSerial() For $i = 1 To UBound($ret) - 1 MsgBox(262144, 'disk', "the serial "& $ret[$i] [0] &" belongs the letter = " & $ret[$i][1]) Next EndFunc ;==>eachUsbDevice Error when I have a USB plugged drawer: For $loDisk In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $partition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") For $loDisk In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $partition^ ERROR Edited August 3, 2014 by Belini My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Link to comment Share on other sites More sharing options...
JScript Posted August 3, 2014 Share Posted August 3, 2014 Hello friend!Which the "OS" that you are testing this code? Which version of your AutoIt?In off: The autoitbrasil is offline for me, see: JS http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!) Somewhere Out ThereJames Ingram Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere! Link to comment Share on other sites More sharing options...
Belini Posted August 4, 2014 Author Share Posted August 4, 2014 Windows XP Spack2, Autoit 3.3.9.4 and forum Autoit Brazil is normal for me. My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Link to comment Share on other sites More sharing options...
j0kky Posted August 4, 2014 Share Posted August 4, 2014 What about using DriveGetDrive DriveGetSerial instead of your script? Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Belini Posted August 4, 2014 Author Share Posted August 4, 2014 (edited) Does not work on all models of stick in some use DriveGetDrive recognizes how hard disk Edited August 4, 2014 by Belini My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Link to comment Share on other sites More sharing options...
j0kky Posted August 4, 2014 Share Posted August 4, 2014 (edited) Maybe >SetupDiGet functions are the solution EDIT: As MSDN documentation reports in GetDriveType reference: To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property Edited August 4, 2014 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Belini Posted August 4, 2014 Author Share Posted August 4, 2014 For my case will not serve why does not specify whether the drive is a flash drive and also does not return the letter-affinity. Class name: DiskDrive Friendly name: General USB Flash Disk USB Device Device Description: Disk drive ; I need to know if it is a removable usb drive My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Link to comment Share on other sites More sharing options...
j0kky Posted August 4, 2014 Share Posted August 4, 2014 (edited) It was just an example of an UDF that uses SetupDiGetDeviceRegistryProperty function but I've written too: To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property Edited August 4, 2014 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Belini Posted August 4, 2014 Author Share Posted August 4, 2014 The code I posted works fine for me and the best fix would be just to not give error when a connected USB drawer. My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Link to comment Share on other sites More sharing options...
j0kky Posted August 4, 2014 Share Posted August 4, 2014 I can't help you about that, I don't know much about WMI scripting Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Belini Posted August 4, 2014 Author Share Posted August 4, 2014 Still thank you for trying to help. My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Link to comment Share on other sites More sharing options...
Belini Posted August 5, 2014 Author Share Posted August 5, 2014 Anyone else have other tips? My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Link to comment Share on other sites More sharing options...
Belini Posted August 8, 2014 Author Share Posted August 8, 2014 Problem solved: expandcollapse popup#include <Array.au3> eachUsbDevice() Func getUsbSerial() Local $colItems, $letter[1], $disk[1], $aArray[1][2] Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2") If IsObj($objWMIService) Then ; <========================= changed here $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", 0x10 + 0x20) For $objitem In $colItems If $objitem.interfacetype = "USB" And $objitem.DeviceId Then; <========================= changed here For $partition In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $objitem.DeviceId & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition") Next If (IsObj($partition) And $partition.DeviceId) Then; <========================= changed here For $loDisk In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $partition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") Next $aRegExSer = StringRegExp($objitem.pnpdeviceid, "\x5C([0-9a-fxA-FX]+)&", 3) If IsArray($aRegExSer) Then _ArrayAdd($disk, $aRegExSer[0]) _ArrayAdd($letter, $loDisk.DeviceId) EndIf EndIf EndIf Next ReDim $aArray[UBound($disk)][2] For $i = 0 To UBound($disk) - 1 $aArray[$i][0] = $disk[$i] $aArray[$i][1] = $letter[$i] Next Return $aArray EndIf EndFunc ;==>getUsbSerial Func eachUsbDevice() Local $ret = getUsbSerial() For $i = 1 To UBound($ret) - 1 MsgBox(262144, 'disk', "the serial " & $ret[$i][0] & " belongs the letter = " & $ret[$i][1]) Next EndFunc ;==>eachUsbDevice Thanks to @joelson0007 forum Autoit Brazil. My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now