Jump to content

USB Drives to Array


Go to solution Solved by kylomas,

Recommended Posts

Hi everyone, this is my first post here!

Considering that I have 2 USB-Drives mounted on my computer.

After running the following script I get 2 MsgBox with their Drive letters. TESTED & WORKING...

What I would like to know is how can I return these 2 values (Drive letters) to an array instead of MsgBox, so I can use them later in a ComboBox.

Basically all I want is a ComboBox with only the USB-Drives Letters and no Floppy nor CD/DVD.

#include <Array.au3>
#include <string.au3>

$usb = DriveGetDrive("REMOVABLE")
If Not @error Then
    For $i = 1 To $usb[0]
        $usbdev = RegRead("HKLM\SYSTEM\MountedDevices", "\DosDevices\" & $usb[$i])
        $usbrealdev = _HexToString(StringReplace($usbdev, "00", ""))
        If StringInStr($usbrealdev, "USBSTOR") Then
            MsgBox(0, "USB-Drive", "USB-Drive Letter: " & StringUpper($usb[$i]))
        EndIf
    Next
EndIf

Any help is highly appreciated.

Thanks in advance ;)

Link to comment
Share on other sites

PauloMatos,

Here is one way...

#include <Array.au3>
#include <string.au3>

local $aUSB[1],$idx = 0

$usb = DriveGetDrive("REMOVABLE")
If Not @error Then
    For $i = 1 To $usb[0]
        $usbdev = RegRead("HKLM\SYSTEM\MountedDevices", "\DosDevices\" & $usb[$i])
        $usbrealdev = _HexToString(StringReplace($usbdev, "00", ""))
        If StringInStr($usbrealdev, "USBSTOR") Then
            $aUSB[$idx] = $usb[$i]
            redim $aUSB[UBound($aUSB)+1]
            $idx += 1
        EndIf
    Next
EndIf

redim $aUSB[$i-1]   ;   eliminate blank ending element

_arraydisplay($aUSB)

kylomas

edit: added array cleanup code

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Solution

Shorter verison eliminating REDIM's...

#include <Array.au3>
#include <string.au3>

local $sOut

$usb = DriveGetDrive("REMOVABLE")
If Not @error Then
    For $i = 1 To $usb[0]
        $usbdev = RegRead("HKLM\SYSTEM\MountedDevices", "\DosDevices\" & $usb[$i])
        $usbrealdev = _HexToString(StringReplace($usbdev, "00", ""))
        If StringInStr($usbrealdev, "USBSTOR") Then $sOut &= $usb[$i] & ','
    Next
EndIf

local $aUSB = stringregexp($sOut,'\w:',3)

_arraydisplay($aUSB)

kylomas

edit: code

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thanks a lot for your reply.

With your code changes I definitly got rid of the floppy drive (GOOD THING) but I can't seem to get the other 2 drives into my ComboBox. 

Would you be so kind to check what's escaping me?

Here's my complete script:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <string.au3>
#include <GuiComboBox.au3>

local $sOut

$usb = DriveGetDrive("REMOVABLE")
If Not @error Then
    For $i = 1 To $usb[0]
        $usbdev = RegRead("HKLM\SYSTEM\MountedDevices", "\DosDevices\" & $usb[$i])
        $usbrealdev = _HexToString(StringReplace($usbdev, "00", ""))
        If StringInStr($usbrealdev, "USBSTOR") Then $sOut &= $usb[$i] & ','
    Next
EndIf

local $aUSB = stringregexp($sOut,'\w:',3)

$aString = _ArrayToString($aUSB, "|", 1)
$aForm1 = GUICreate("", 400, 200)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
$aGroup1 = GUICtrlCreateGroup("Select which USB Drive to move the files.", 50, 16, 300, 90)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
$aDriveSelect1 = GUICtrlCreateCombo("", 60, 35, 280, 50, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, StringUpper($aString))
GUICtrlSetFont($aDriveSelect1, 20, 600)
_GUICtrlComboBox_SetItemHeight($aDriveSelect1, 50, 0)
_GUICtrlComboBox_SetItemHeight($aDriveSelect1, 50, -1)
GUISetState(@SW_SHOW)
$aButton1 = GUICtrlCreateButton("Move", 88, 130, 105, 49)
$aButton2 = GUICtrlCreateButton("Exit", 200, 130, 105, 49)

While 1
    $aMsg = GUIGetMsg()
    Select
        Case $aMsg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $aMsg = $aButton1
            $d = GUICtrlRead($aDriveSelect1)
            $a = GUICreate("", 400, 150)
            $t = GUICtrlCreateLabel("You selected this USB Drive:   " & StringUpper($d) & " " & DriveGetLabel($d) & @CRLF & "Are you sure you want to move the files to this USB-Drive?", 25, 15, 300, 50)
            GUICtrlSetFont(-1, 11, 400)
            $y = GUICtrlCreateButton("YES", 88, 80, 105, 49)
            $n = GUICtrlCreateButton("NO", 200, 80, 105, 49)
            GUISetState(@SW_SHOW)
            While 1
                $bMsg = GUIGetMsg()
                Select
                    Case $bMsg = $GUI_EVENT_CLOSE
                        Exit
                    Case $bMsg = $y
                        MsgBox(0, "Moving Files...", "Moving files to: " & @CRLF & StringUpper($d) & " " & DriveGetLabel($d)) ; Changing to FileMove Command once I get the ComboBox working.
                        Exit
                    Case $bMsg = $n
                        Exit
                EndSelect
            WEnd
        Case $aMsg = $aButton2
            Exit
    EndSelect
WEnd

Thanks once again for your readiness ;)

EDIT:

I just found my error.

Instead of:

$aString = _ArrayToString($aUSB, "|", 1)

I just need:

$aString = _ArrayToString($aUSB)

Now its working fine! 

Thanks a lot for your help.

Cheers  :thumbsup:

N

Edited by PauloMatos
Link to comment
Share on other sites

Hi PauloMatos,

Your code is not picking up all of my USB devices.  If all you want to do is populate the Combo control then let's eliminate the whole array deal like this...

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <string.au3>
#include <GuiComboBox.au3>

local $sOut = ''

$usb = DriveGetDrive("REMOVABLE")
If Not @error Then
    For $i = 1 To $usb[0]
        $usbdev = RegRead("HKLM\SYSTEM\MountedDevices", "\DosDevices\" & $usb[$i])
        $usbrealdev = _HexToString(StringReplace($usbdev, "00", ""))
        If StringInStr($usbrealdev, "USBSTOR") Then $sOut &= $usb[$i] & '|'
    Next
EndIf

ConsoleWrite($sOut & @CRLF)

;local $aUSB = stringregexp($sOut,'\w:',3)
;$aString = _ArrayToString($aUSB, "|", 1)

$aForm1 = GUICreate("", 400, 200)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
$aGroup1 = GUICtrlCreateGroup("Select which USB Drive to move the files.", 50, 16, 300, 90)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
$aDriveSelect1 = GUICtrlCreateCombo(stringleft($sOut,2), 60, 35, 280, 50, $CBS_DROPDOWNLIST)

GUICtrlSetData(-1, StringUpper($sOut))
;GUICtrlSetData(-1, StringUpper($aString))

GUICtrlSetFont($aDriveSelect1, 20, 600)
_GUICtrlComboBox_SetItemHeight($aDriveSelect1, 50, 0)
_GUICtrlComboBox_SetItemHeight($aDriveSelect1, 50, -1)
GUISetState(@SW_SHOW)
$aButton1 = GUICtrlCreateButton("Move", 88, 130, 105, 49)
$aButton2 = GUICtrlCreateButton("Exit", 200, 130, 105, 49)

While 1
    $aMsg = GUIGetMsg()
    Select
        Case $aMsg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $aMsg = $aButton1
            $d = GUICtrlRead($aDriveSelect1)
            $a = GUICreate("", 400, 150)
            $t = GUICtrlCreateLabel("You selected this USB Drive:   " & StringUpper($d) & " " & DriveGetLabel($d) & @CRLF & "Are you sure you want to move the files to this USB-Drive?", 25, 15, 300, 50)
            GUICtrlSetFont(-1, 11, 400)
            $y = GUICtrlCreateButton("YES", 88, 80, 105, 49)
            $n = GUICtrlCreateButton("NO", 200, 80, 105, 49)
            GUISetState(@SW_SHOW)
            While 1
                $bMsg = GUIGetMsg()
                Select
                    Case $bMsg = $GUI_EVENT_CLOSE
                        Exit
                    Case $bMsg = $y
                        MsgBox(0, "Moving Files...", "Moving files to: " & @CRLF & StringUpper($d) & " " & DriveGetLabel($d)) ; Changing to FileMove Command once I get the ComboBox working.
                        Exit
                    Case $bMsg = $n
                        Exit
                EndSelect
            WEnd
        Case $aMsg = $aButton2
            Exit
    EndSelect
WEnd

kylomas

edit: added code to show first selection in combo when displayed

edits: Incidentally, the code you posted was using _arraytostring starting at offset 1 and was missing the first entry.  Regexp does NOT return an element count at offset 0.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Not sure why but that answer doesnt work for me

The script finishes but no display of usb

AutoIt3Wrapper v.2.1.4.4 SciTE v.3.3.7.0 ;  Keyboard:00000809  OS:WIN_81/  CPU:X64 OS:X64

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...