Jump to content

Write file to selected USB device


James
 Share

Recommended Posts

Hey all,

OK, my secret project is about USB devices :).

I have a little function which will write the USB device names to a combo box.

E.g:

USB1

USB2

I then have a button which will create a autorun.inf file on the selected USB drive name:

Func WriteData()
    $ReadDevice = GUICtrlRead($USBDevices)
    $Folder = "\AUSB-Data\"
    If FileExists($ReadDevice) Then
        ;~ autorun.inf exists do nothing
    Else
        $AutoFile = FileOpen($ReadDevice & "\autorun.inf", 1)
        FileWrite($AutoFile, "[autorun]" & @CRLF & "open=" & $Folder & "main.exe")
        FileClose($AutoFile)
        MsgBox(0, "AUSB", "autorun.inf has now been written to " & $ReadDevice)
    EndIf
EndFunc

My problem is that the file isnt written to the selected drive name. So how would I get the drive letter of the selected drive label?

Thanks,

-James

Link to comment
Share on other sites

I did this for a program that never knew what drive letter would be assigned to a USB drive. Not real elegant but it proved reliable.

Case $msg = $copysurveysitem

$ret = ("If No Removable Drive is attached to the system the Program will EXIT")

GUICtrlSetData($copysurveyprogress, "" & $ret)

Sleep(3000)

While 2

; Checks drives F - M for the presence of a removable drive. Once found it causes this loop to exit whereupon the program will

; comtinue While 1.

$var3 = DriveGetType( "F:\" )

If $var3 = "Removable" then ExitLoop

$var4 = DriveGetType( "G:\" )

If $var4 = "Removable" then ExitLoop

$var5 = DriveGetType( "H:\" )

If $var5 = "Removable" then ExitLoop

$var6 = DriveGetType( "I:\" )

If $var6 = "Removable" then ExitLoop

$var7 = DriveGetType( "J:\" )

If $var7 = "Removable" then ExitLoop

$var8 = DriveGetType( "K:\" )

If $var8 = "Removable" then ExitLoop

$var9 = DriveGetType( "L:\" )

If $var9 = "Removable" then ExitLoop

$var10 = DriveGetType( "M:\" )

If $var10 = "Removable" then ExitLoop

; If the above Checks drives F - M for the presence of a removable drive and does not find it, then this will continue.

; It's purpose is to confirm that there is no such removable drive whereupon it causes the program to END.

$var3 = DriveGetType( "F:\" )

If $var3 <> "Removable" then Exit

$var4 = DriveGetType( "G:\" )

If $var4 <> "Removable" then Exit

$var5 = DriveGetType( "H:\" )

If $var5 <> "Removable" then Exit

$var6 = DriveGetType( "I:\" )

If $var6 <> "Removable" then Exit

$var7 = DriveGetType( "J:\" )

If $var7 <> "Removable" then Exit

$var8 = DriveGetType( "K:\" )

If $var8 <> "Removable" then Exit

$var9 = DriveGetType( "L:\" )

If $var9 <> "Removable" then Exit

$var10 = DriveGetType( "M:\" )

If $var10 <> "Removable" then Exit

WEnd

Then the program continued like this....

$var = DriveGetType( "F:\" )

If $var = ("Removable") then FileMove("c:\temp\survey\*.dat", "F:\Surveys\*.*", 8)

$var = DriveGetType( "G:\" )

If $var = ("Removable") then FileMove("c:\temp\survey\*.dat", "G:\Surveys\*.*", 8)

$var = DriveGetType( "H:\" )

If $var = ("Removable") then FileMove("c:\temp\survey\*.dat", "H:\Surveys\*.*", 8)

$var = DriveGetType( "I:\" )

If $var = ("Removable") then FileMove("c:\temp\survey\*.dat", "I:\Surveys\*.*", 8)

$var = DriveGetType( "J:\" )

If $var = ("Removable") then FileMove("c:\temp\survey\*.dat", "J:\Surveys\*.*", 8)

$var = DriveGetType( "K:\" )

If $var = ("Removable") then FileMove("c:\temp\survey\*.dat", "K:\Surveys\*.*", 8)

$var = DriveGetType( "L:\" )

If $var = ("Removable") then FileMove("c:\temp\survey\*.dat", "L:\Surveys\*.*", 8)

$var = DriveGetType( "M:\" )

If $var = ("Removable") then FileMove("c:\temp\survey\*.dat", "M:\Surveys\*.*", 8)

Hope this helps...

Hey all,

OK, my secret project is about USB devices :).

I have a little function which will write the USB device names to a combo box.

E.g:

USB1

USB2

I then have a button which will create a autorun.inf file on the selected USB drive name:

Func WriteData()
    $ReadDevice = GUICtrlRead($USBDevices)
    $Folder = "\AUSB-Data\"
    If FileExists($ReadDevice) Then
        ;~ autorun.inf exists do nothing
    Else
        $AutoFile = FileOpen($ReadDevice & "\autorun.inf", 1)
        FileWrite($AutoFile, "[autorun]" & @CRLF & "open=" & $Folder & "main.exe")
        FileClose($AutoFile)
        MsgBox(0, "AUSB", "autorun.inf has now been written to " & $ReadDevice)
    EndIf
EndFunc

My problem is that the file isnt written to the selected drive name. So how would I get the drive letter of the selected drive label?

Thanks,

-James

Link to comment
Share on other sites

Recap:

My script gets the label of any removable devices and lists them in a combo box. I then need to read the available drive names and get the corresponding drive letter to its name.

Retrieving removable drives and then listing them:

Func GetDevices($io_type)
    ;~ This function will get all USB devices and list them
    MsgBox(0, "AUSB", "AUSB will now load your USB devices." & @CRLF & "This may take some time!", 3)
    For $a = 1 To $io_type[0]
        $DriveLabel = DriveGetLabel($io_type[$a])
        GUICtrlSetData($USBDevices, $DriveLabel)
    Next
    GUISetState(@SW_SHOW)
    MsgBox(0, "AUSB", "USB devices have now loaded.", 3)
EndFuncoÝ÷ ÙhZ´Z½ë(}ªß¢¼+ا֭jÚ-ëç-y×k÷ºÚ"µÍ[ÈÜ]Q]J
BIÌÍÔXY]XÙHHÕRPÝXY
    ÌÍÕTÐ]XÙÊBIÌÍÑÛH  ][ÝÉÌLÐUTÐQ]IÌLÉ][ÝÂIÌÍÑ]UHH]QÙ]]J ÌÍÔXY]XÙJBRY[Q^ÝÊ ÌÍÔXY]XÙJH[BNß]]Ü[[^ÝÈÈÝ[ÂQ[ÙBBIÌÍÐ]]Ñ[HH[SÜ[  ÌÍÑ]UH   [È ][ÝÉÌLØ]]Ü[[][ÝËJBBQ[UÜ]J   ÌÍÐ]]Ñ[K    ][ÝÖØ]]Ü[I][ÝÈ    [ÈÔ   [È ][ÝÛÜ[I][ÝÈ    [È ÌÍÑÛ    [È ][ÝÛXZ[^I][ÝÊBBQ[PÛÜÙJ   ÌÍÐ]]Ñ[JBBSÙÐÞ
    ][ÝÐUTÐ][ÝË    ][ÝØ]]Ü[[ÈÝÈY[Ü][È  ][ÝÈ  [È ÌÍÔXY]XÙJBQ[Y[[
Link to comment
Share on other sites

I thought I could try and split the data I read:

Func WriteData()
    $ReadDevice = GUICtrlRead($USBDevices)
    $Folder = "\AUSB-Data\"
    $DriveType = DriveGetDrive($ReadDevice)
    $DeviceSplit = StringSplit($DriveType, ":")
    $StringLen = StringLen($DriveLabel)
    _StringInsert($DeviceSplit[1], "\", $StringLen)
    If FileExists($ReadDevice) Then
        ;~ autorun.inf exists do nothing
    Else
        $AutoFile = FileOpen($DeviceSplit[1] & "\autorun.inf", 1)
        FileWrite($AutoFile, "[autorun]" & @CRLF & "open=" & $Folder & "main.exe")
        FileClose($AutoFile)
        If @error Then
            MsgBox(0, "AUSB", "autorun.inf could not be created on drive: " & $ReadDevice)
        Else
        MsgBox(0, "AUSB", "autorun.inf has now been written to " & $ReadDevice)
        EndIf
    EndIf
EndFunc

But it didnt work like I thought it would :)

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...