Jump to content

AutoIT Scripting help


Recommended Posts

Hello,

I have an AutoIT script that detects usb drive insertion. I'm trying to figure out how to copy files to the flash drive from the computer without the script actually knowing the drive name. Below is the script. you can see where i have implemented the copy to function, but without the script knowing the drive its hard to tell it where to copy to.

Copy Function:

$b = True
If $b = True Then FileInstall("C:\location\test.bmp", "FLASHDRIVE\test.bmp")

USB detection with copy script:

Dim $DBT_DEVICEARRIVAL = "0x00008000"
Dim $DBT_DEVICECOMPLETEREMOVAL = "0x00008004"
Dim $USB_ATTENTION = "0x00000007"
Dim $WM_DEVICECHANGE = 0x0219
Dim $Drives
Dim $Drive_Type = "ALL"  ; Set to ALL because some USB Drives are detected as Fixed Disks, and we don't want to miss those
Dim $WATCH = False
Dim $MyDrive = "STUFF"

;Get Initial List of Drives to Check Against
UpdateDrives()
;Setup The GUI to watch for the DeviceChange Event
GUICreate("")
GUIRegisterMsg($WM_DEVICECHANGE, "DeviceChange")
Func DeviceChange($hWndGUI, $MsgID, $WParam, $LParam)
    Switch $WParam
        Case $USB_ATTENTION
        ; This only happens when USB drives are inserted, so I use it to tell the difference between these and CDROMs
            $WATCH = True
        Case $DBT_DEVICECOMPLETEREMOVAL
        ; Whenever a Drive is Removed, Update the Drive List
            UpdateDrives()
        Case $DBT_DEVICEARRIVAL
        ; A drive was inserted
        ; Use $WATCH to tell if this was a CDROM or USB
        ; $WATCH = True, USB
        ; $WATCH = False, CDROM
            If $WATCH = True Then
            ; New USB Drive Was Detected, Time to Find it's Letter
                $New = FindNewDrive(); $New now has the Drive Letter of our New Drive, so USE IT!!!
                $Label = DriveGetLabel($New)

	$b = True
	If $b = True Then FileInstall("C:\Location\test.bmp", ("$MyDrive\Destination\test.bmp")

	EndIf
            ; Now Reset Drive List so more insertions can also be detected accurately
                UpdateDrives()
    EndSwitch
EndFunc  ;==>DeviceChange

; This just jumps through the new Drive List, comparing them until it finds the entry that is in the new one that isn't in the old one
Func FindNewDrive()
    $Temp = DriveGetDrive( "REMOVABLE" )
    For $i = 1 to $Temp[0]
        $Old = False
        For $j = 1 to $DRIVES[0]
            If $DRIVES[$j] == $Temp[$i] Then $Old = True
        Next
        If $Old == False Then Return $Temp[$i]     
    Next
EndFunc  ;==>FindNewDrive

; Just to keep things neat, and so if Variables Ever Change, this makes updating easier
Func UpdateDrives()
    $Drives = DriveGetDrive( $Drive_Type )
EndFunc     ;==>UpdateDrives

; Main Loop to Keep the Program Open
; No Real Way of ending this program, except for just killing the process
; Which is what I want, an always on backup for my drive every time I insert it
While 1
    $GuiMsg = GUIGetMsg()
; This is needed because the watch event above not only triggers before a USB Drive is inserted/removed,
; but also AFTER insertion too, and if not reset, a subsequent CD insertion will trigger it again.
; So, every second, we reset $WATCH, to keep things clean
    Sleep (1000)
    $WATCH = False
WEnd

Micah C

Link to comment
Share on other sites

Given that your function FindNewDrive() works, and finds the newly inserted drive, then all you need to do is:

Replace:

$New = FindNewDrive(); $New now has the Drive Letter of our New Drive, so USE IT!!!
$Label = DriveGetLabel($New)

$b = True
If $b = True Then FileInstall("C:\Location\test.bmp", ("$MyDrive\Destination\test.bmp")

with

$New = FindNewDrive(); $New now has the Drive Letter of our New Drive, so USE IT!!!
$Label = DriveGetLabel($New)
FileInstall("C:\Location\test.bmp", ($New & "\Destination\test.bmp")

Though check ($New & "\Destination\test.bmp"), im not sure what AutoIT concatenation symbol is, but it looks VB, and if i remember correctly VB use & (could be +).

Link to comment
Share on other sites

Thanks for your response. I tried replacing the code as instructed, but unfortunately i still get an error. "Error parsing function call." the error seems to be located ($New + "\test.bmp"^ERROR. I have tried using the & and the + signs to concatenate, although still receive errors. I looked up concatenation symbols, and parsing functions on Google, but it looks as if you were right about the "+" sign. Any suggestions or help here?

Link to comment
Share on other sites

downloaded AutoIT, and got it working, issue with your FileInstall function, you opened a brace, but never closed it, but reading up, i found fileCopy works better.

If $WATCH = True Then
; New USB Drive Was Detected, Time to Find it's Letter
    $New = FindNewDrive()
    $Label = DriveGetLabel($New)
    $MyDrive = $New & "\"
    FileCopy("C:\test.bmp", $MyDrive)

EndIf

Heres the copy i have working.

Dim $DBT_DEVICEARRIVAL = "0x00008000"
Dim $DBT_DEVICECOMPLETEREMOVAL = "0x00008004"
Dim $USB_ATTENTION = "0x00000007"
Dim $WM_DEVICECHANGE = 0x0219
Dim $Drives
Dim $Drive_Type = "ALL"; Set to ALL because some USB Drives are detected as Fixed Disks, and we don't want to miss those
Dim $WATCH = False
Dim $MyDrive = "STUFF"

;Get Initial List of Drives to Check Against
UpdateDrives()
;Setup The GUI to watch for the DeviceChange Event
GUICreate("")
GUIRegisterMsg($WM_DEVICECHANGE, "DeviceChange")
Func DeviceChange($hWndGUI, $MsgID, $WParam, $LParam)
Switch $WParam
Case $USB_ATTENTION
; This only happens when USB drives are inserted, so I use it to tell the difference between these and CDROMs
$WATCH = True
Case $DBT_DEVICECOMPLETEREMOVAL
; Whenever a Drive is Removed, Update the Drive List
UpdateDrives()
Case $DBT_DEVICEARRIVAL
; A drive was inserted
; Use $WATCH to tell if this was a CDROM or USB
; $WATCH = True, USBs
; $WATCH = False, CDROM
If $WATCH = True Then
; New USB Drive Was Detected, Time to Find it's Letter
    $New = FindNewDrive()
    $Label = DriveGetLabel($New)
    $MyDrive = $New & "\"
    FileCopy("C:\test.bmp", $MyDrive)

EndIf
; Now Reset Drive List so more insertions can also be detected accurately
UpdateDrives()
EndSwitch
EndFunc;==>DeviceChange

; This just jumps through the new Drive List, comparing them until it finds the entry that is in the new one that isn't in the old one
Func FindNewDrive()
$Temp = DriveGetDrive( "REMOVABLE" )
For $i = 1 to $Temp[0]
$Old = False
For $j = 1 to $DRIVES[0]
If $DRIVES[$j] == $Temp[$i] Then $Old = True
Next
If $Old == False Then Return $Temp[$i]
Next
EndFunc;==>FindNewDrive

; Just to keep things neat, and so if Variables Ever Change, this makes updating easier
Func UpdateDrives()
$Drives = DriveGetDrive( $Drive_Type )
EndFunc;==>UpdateDrives

; Main Loop to Keep the Program Open
; No Real Way of ending this program, except for just killing the process
; Which is what I want, an always on backup for my drive every time I insert it
While 1
$GuiMsg = GUIGetMsg()
; This is needed because the watch event above not only triggers before a USB Drive is inserted/removed,
; but also AFTER insertion too, and if not reset, a subsequent CD insertion will trigger it again.
; So, every second, we reset $WATCH, to keep things clean
Sleep (1000)
$WATCH = False
WEnd

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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