VBA 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
End With
Exit Do
Case "__InstanceDeletionEvent"
MsgBox "A file was just deleted: " & _
objEventObject.TargetInstance.PartComponent
Exit Do
Case "__InstanceModificationEvent"
MsgBox "A file was just modified: " & _
objEventObject.TargetInstance.PartComponent
Exit Do
End Select
Loop
Hello,
I currently use a variation of the above code to monitor a folder for a new file being created in it.
I was wondering it if was possibly (and if so how) to add a 2nd location to search.