问题
Microsoft disabled autorun. However to run a scan and backup files (defined in file.cmd
), I found this AutoIt script:
$DBT_DEVICEARRIVAL = "0x00008000"
$WM_DEVICECHANGE = 0x0219
GUICreate("")
GUIRegisterMsg($WM_DEVICECHANGE , "MyFunc")
Func MyFunc($hWndGUI, $MsgID, $WParam, $LParam)
If $WParam == $DBT_DEVICEARRIVAL Then
MsgBox(4096, "Info", "My Drive has been Inserted, Backup My Files!")
EndIf
EndFunc
While 1
$GuiMsg = GUIGetMsg()
WEnd
It displays a message box whenever the USB drive, on which the script is, gets plugged in. I compiled and copied it to my USB drive. Soon as I plugged it in, the MsgBox()
appeared.
I replaced:
MsgBox(4096, "Info", "My Drive has been Inserted, Backup My Files!")
with:
Run ("F:\path\to\my\file.cmd")
.
But other computers assign a different drive letter to the USB drive. How can I edit the script so running file.cmd
doesn't require the drive letter F:
assigned? I am completely OK if someone can translate this into Python.
回答1:
You will need to cycle thru all removable drives and search for the file you need to run.
$DBT_DEVICEARRIVAL = "0x00008000"
$WM_DEVICECHANGE = 0x0219
GUICreate("")
GUIRegisterMsg($WM_DEVICECHANGE , "MyFunc")
While True
$GuiMsg = GUIGetMsg()
WEnd
Func MyFunc($hWndGUI, $MsgID, $WParam, $LParam)
If $WParam == $DBT_DEVICEARRIVAL Then
$Drives = DriveGetDrive( "REMOVABLE" )
For $i = 1 to $Drives[0]
$file = $Drives[$i] & "\path\to\my\file.cmd"
If FileExists($file) Then Run($file)
Next
EndIf
EndFunc
来源:https://stackoverflow.com/questions/35252893/run-when-usb-is-mounted