Script Recipe of the week: Detecting a new disk inserted in the system


Calvin Hsia presented a method to detect a device arrival in Visual Fox Pro. The code looks pretty long, even for a C++ programmer like me 🙂


I am wondering if there is a shorter variant… Here is a similar code VBScript, which uses WMI events:


 Set objWMIService = GetObject(_ 
“winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2”)
Set colMonitoredEvents = objWMIService.ExecNotificationQuery( _
“Select * from Win32_VolumeChangeEvent”)
Do Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo objLatestEvent.DriveName
Wscript.Echo objLatestEvent.EventType
Wscript.Echo objLatestEvent.Time_Created
Loop

Comments (0)