This function returns an [x][2] array, where [x][0]=drive number and [x][1]=drive size. The length of the array is in [0][0]
Notice that I return drive numbers, not letters. Letters are too ambigious - a physical drive can have multiple letters (partitions), no letter (if windows doesn't recognise the file system, or if it's letter has been removed), or a letter can represent multiple physical drives (spanned or stripped volumes).
Also note that drive numbers are 0-based.
Func DriveGetUsbList()
dim $i, $temp
;Step 1 - Get all valid drive numbers
Dim $DriveListNum[100][2], $DriveListNext=1, $DriveListText
;$DriveListNum is a table of valid drive numbers and their sizes, $DriveListNext is an index to the next free entry
;$DriveListText is a temporary variable used for parsing text in and out
$DriveListText=StringSplit(RunGetOutput(@ComSpec & " /c echo list disk | diskpart"), @CRLF)
For $i = 1 to $DriveListText[0]
if StringInStr($DriveListText[$i], "Disk ") Then
$Temp = StringMid($DriveListText[$i],8,3)
if $Temp<>"###" Then
$DriveListNum[$DriveListNext][0] = Int($Temp)
$DriveListNum[$DriveListNext][1] = StringMid($DriveListText[$i],25,7)
$DriveListNext = $DriveListNext + 1
EndIf
If $DriveListNext > Ubound($DriveListNum) Then Redim $DriveListNum[Ubound($DriveListNum)+20][2]
EndIf
Next
$DriveListNum[0][0]=$DriveListNext ;Element [0][0] contains the list size (1-based)
;Step 2 - figure out which are attached via USB
Dim $TempFile, $hndFile_Temp, $DriveUsbCur
;Step 2.1 - build a script for DiskPart
$TempFile = _TempFile()
$hndFile_Temp = FileOpen($TempFile, 2)
for $i = 1 to $DriveListNext - 1
FileWriteLine($hndFile_Temp, "Select Disk " & $DriveListNum[$i][0])
FileWriteLine($hndFile_Temp, "Detail Disk")
Next
FileClose($hndFile_Temp)
;Step 2.2 - execute the script for DiskPart
$DriveListText=StringSplit(RunGetOutput(@ComSpec & " /c echo list disk | diskpart < " & $TempFile), @CRLF)
;Step 2.3 - parse the results, build a list of USB drives
Dim $DriveListUsb[$DriveListNext-1][2]
$DriveListNext=1
For $i = 1 to $DriveListText[0]
if StringRight($DriveListText[$i], 26) = ' is now the selected disk.' Then
$DriveUsbCur = StringSection($DriveListText[$i], 2, " ")
EndIf
if $DriveListText[$i] = 'Type : USB' Then
$DriveListUsb[$DriveListNext][0] = $DriveUsbCur
$DriveListUsb[$DriveListNext][1] = TableLookUp($DriveListNum, $DriveUsbCur)
$DriveListNext = $DriveListNext + 1
EndIf
Next
$DriveListUsb[0][0]=$DriveListNext-1
;Step 3 - Return the results table
If $DriveListUsb[0][0] > 0 Then
Redim $DriveListUsb[$DriveListUsb[0][0]+1][2]
Return $DriveListUsb
Else
Return 0
EndIf
EndFunc





