0

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?

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
Sourabh
  • 21
  • 1
  • Not really possible with a batch file alone. You need some means to [monitor for USB events](http://stackoverflow.com/questions/620144/detecting-usb-drive-insertion-and-removal-using-windows-service-and-c-sharp). Bottom line: executing the batch file is trivial, determining when to execute the batch file is not. – Bacon Bits Oct 03 '15 at 07:52
  • 2
    1. please select the correct tags for your question, as [tag:batch-file] and [tag:spring-batch] are totally different things; tag [tag:batch-processing] does not apply at all; 2. share what you have tried so far and precisely describe where you are stuck; remember that SO is not a free code writing service; – aschipfl Oct 03 '15 at 08:27
  • Should be migrated to superuser.com – wOxxOm Oct 03 '15 at 21:05

1 Answers1

1

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.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • i am using windows 8.1 .and Windows Driver Foundation - User-mode Driver Framework isn't running...so what to do now? @wOxxOm – Sourabh Oct 04 '15 at 13:28
  • @Sourabh, I don't understand what is the problem and why haven't tried [googling it](https://www.google.com/#q=how+to+start+windows+service). – wOxxOm Oct 04 '15 at 13:46