I quote below some code from Mr Ivan Moala. This code loops out of the script when an event--a file found, deleted or modified in a directory-- becomes true. Would someone be able to show me how to make this run continuously? I thought of removing the Do element and having it just go back to the beginning but apparently goto is not part of vbscript. Thanks.
Code:
Dim MyExcel
Set MyExcel = GetObject("C:\ExcelFiles\Useful\MonitorDirectory.xls")
''
strComputer = "."
'// Note 4 forward slashes!
strDirToMonitor = "C:\\\\A_BOOK"
'// Monitor Above every 10 secs...
strTime = "10"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN " & strTime & " WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=" & Chr(34) & strDirToMonitor & Chr(34) & "'")
Do While True
Set objEventObject = colMonitoredEvents.NextEvent()
Select Case objEventObject.Path_.Class
Case "__InstanceCreationEvent"
MsgBox "A new file was just created: " & _
objEventObject.TargetInstance.PartComponent
MyFile = StrReverse(objEventObject.TargetInstance.PartComponent)
'// Get the string to the left of the first \ and reverse it
MyFile = (StrReverse(Left(MyFile, InStr(MyFile, "\") - 1)))
MyFile = Mid(MyFile, 1, Len(MyFile) - 1)
With MyExcel.Worksheets(1)
.Range("A65536").End(-4162).Offset(1, 0).Value = MyFile
End With
Exit Do
Case "__InstanceDeletionEvent"
MsgBox "A file was just deleted: " & _
objEventObject.TargetInstance.PartComponent
MyFile = StrReverse(objEventObject.TargetInstance.PartComponent)
'// Get the string to the left of the first \ and reverse it
MyFile = (StrReverse(Left(MyFile, InStr(MyFile, "\") - 1)))
MyFile = Mid(MyFile, 1, Len(MyFile) - 1)
With MyExcel.Worksheets(1)
.Range("A65536").End(-4162).Offset(1, 0).Value = MyFile
End With
Exit Do
Case "__InstanceModificationEvent"
MsgBox "A file was just modified: " & _
objEventObject.TargetInstance.PartComponent
Exit Do
End Select
Loop