Sourabh
Sourabh

Reputation: 21

Autorun batch file through eject command

Is it possible for a batch script on my local machine (e.g. in D:\ Batch files\1.bat) to run after I insert or eject a USB drive?

Upvotes: 0

Views: 955

Answers (1)

woxxom
woxxom

Reputation: 73576

Preliminaries:

  • Right-click Computer icon, select Manage, go to Services,
    select Windows Driver Foundation - User-mode Driver Framework and make sure it's running and its Startup Type is set to Automatic or Automatic (Delayed Start).

Now go to Task Scheduler:

  1. Make a new task, name it MyCoolBatch on USB-connect.
  2. On Triggers tab add a new trigger:

    • On an event in the first dropdown box
    • Microsoft-Windows-DriverFrameworks-UserMode/Operational in the Log dropdown
    • DriverFrameworks-UserMode in the Source
    • 2003 in the Event ID (this is USB connect event)

    new trigger screenshot

  3. On Actions tab make a new action and select your batch file.
    You can add a parameter like plugged.

Repeat steps 1-3 with an Event ID 2100 (the USB disconnect event) and a batch file parameter like ejected so, assuming the same batch file is used, you can check if device was connected/ejected:

if "%1"=="plugged" .........
if "%1"=="ejected" .........

Based on The Windows 7 Event Log and USB Device Tracking article.

Upvotes: 1

Related Questions