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?
Asked
Active
Viewed 813 times
0

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
-
21. 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 Answers
1
Preliminaries:
- Right-click
Computer
icon, selectManage
, go toServices
,
selectWindows Driver Foundation - User-mode Driver Framework
and make sure it's running and its Startup Type is set toAutomatic
orAutomatic (Delayed Start)
.
Now go to Task Scheduler
:
- Make a new task, name it
MyCoolBatch on USB-connect
. On
Triggers
tab add a new trigger:On an event
in the first dropdown boxMicrosoft-Windows-DriverFrameworks-UserMode/Operational
in the Log dropdownDriverFrameworks-UserMode
in the Source2003
in the Event ID (this is USB connect event)
On
Actions
tab make a new action and select your batch file.
You can add a parameter likeplugged
.
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