Hi, I am working on a small script here to detect a flash drive and then run a backup file (batch file). It works pretty well the first time around when I plug in the correct drive that has the label I am requiring, but when I plug in another drive, remove it and plug the correct one back in, it fails on me and shows "Object Required" which points to my batch file, like it cannot find it the 2nd time.
Can anyone tell me where I am going wrong? Thanks for any help...
Can anyone tell me where I am going wrong? Thanks for any help...
Code:
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colEvents = objWMIService.ExecNotificationQuery _
("Select * From __InstanceOperationEvent Within 10 Where " _
& "TargetInstance isa 'Win32_LogicalDisk'")
Do While True
Set objEvent = colEvents.NextEvent
If objEvent.TargetInstance.DriveType = 2 Then
Select Case objEvent.Path_.Class
Case "__InstanceCreationEvent"
Wscript.Echo "Flash Drive " & objEvent.TargetInstance.DeviceId & _
" has been added to the system."
'get label name
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'R:'")
For Each objItem in colItems
'Wscript.Echo objItem.VolumeName
If objItem.VolumeName = "KINGSTON64" Then
'MsgBox"Correct Flash Drive Inserted"
objShell.Run("c:\Users\mike\desktop\backups.bat")
Set objShell = Nothing
Else
MsgBox"Incorrect Backup Drive, No backup will be performed!", vbExclamation
End If
Next
Case "__InstanceDeletionEvent"
Wscript.Echo "Drive " & objEvent.TargetInstance.DeviceId & _
" has been removed from the system."
End Select
End If
Loop