The following vbScript works fine at monitoring a folder for new file additions; and I need it to do so continuously, which it does. The problem is that it loops through the If statement more than once and creates all kinds of problems by trying to run macros more than once. Even in the example below, I get 2 message boxes, no matter how long I ask it to sleep. Does anyone have a suggestion on how to make it loop through once only for each folder change event? Thank you.
Code:
Dim xlApp
Set xlApp = CreateObject("Excel.Application")
strComputer = "."
strDirToMonitor = "C:\\\\srvr\\\\ftp\\\\rie\\\\incoming"
strTime = "2"
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) & "'")
Wscript.Echo vbCrlf & Now & vbTab & _
"Begin Monitoring for a Folder Change Event..." & vbCrlf
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
If objLatestEvent.Path_.Class = "__InstanceCreationEvent" Then
MsgBox "Hello"
wscript.Sleep 10000
End If
Loop