Micah C Posted May 1, 2009 Share Posted May 1, 2009 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 Quote Link to comment Share on other sites More sharing options...
Deveant Posted May 2, 2009 Share Posted May 2, 2009 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 +). Quote Link to comment Share on other sites More sharing options...
Micah C Posted May 3, 2009 Author Share Posted May 3, 2009 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? Quote Link to comment Share on other sites More sharing options...
Deveant Posted May 4, 2009 Share Posted May 4, 2009 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 Quote Link to comment Share on other sites More sharing options...
Micah C Posted May 6, 2009 Author Share Posted May 6, 2009 Awesome, It works! Thanks for the help with this. This could also work vise-versa from Drive to PC with the @working dir. Anyway, thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.