Run when USB is mounted [duplicate]

人盡茶涼 提交于 2019-12-13 08:55:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!