• Happy holidays, folks! Thanks to each and every one of you for being part of the Tom's Hardware community!

Run task automatically upon removal of drive?

monster47

Honorable
Oct 29, 2013
24
0
10,510
Hello, I was wondering if there is any way to automatically run a task upon the removal of a flash drive. for example, if i wanted a batch file to run as soon as i remove a flash drive, is it possible?
Thank you.
 
Solution
I offer this vbs script I wrote a long time ago, don't ask me for any details or support. But notice DEVICE LEFT:



================================================================
' test computer's fitness to detect DeviceChange events. Self terminates in 5 minutes.
'
Dim beginTime, endTime, elapsed
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService. _
ExecNotificationQuery( _
"Select * from Win32_VolumeChangeEvent")
WScript.Echo "Begin Test"
beginTime = Now
Do
Set...
I offer this vbs script I wrote a long time ago, don't ask me for any details or support. But notice DEVICE LEFT:



================================================================
' test computer's fitness to detect DeviceChange events. Self terminates in 5 minutes.
'
Dim beginTime, endTime, elapsed
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService. _
ExecNotificationQuery( _
"Select * from Win32_VolumeChangeEvent")
WScript.Echo "Begin Test"
beginTime = Now
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
'Wscript.Echo objLatestEvent.DriveName
'Wscript.Echo objLatestEvent.EventType
'Wscript.Echo objLatestEvent.Time_Created

Select Case objLatestEvent.EventType

Case 2 If ( fso.FolderExists( objLatestEvent.DriveName & "\Ipod_Control" ) ) then _
WScript.Echo "Device arrived and iPod detected as drive " & objLatestEvent.DriveName
Case 3 WScript.Echo "Device left"
End Select

endTime = Now
elapsed = Round( (endTime-beginTime)*100000,0 )
If elapsed > 360 Then WScript.Quit()

Loop until ( MsgBox("Do it again?", vbYesNo, "Test" )=7 )
================================================================
 
Solution