Home All Groups Group Topic Archive Search About

Monitor SubFolder Creation in Multiple Folders



Author
13 Mar 2007 8:12 PM
Highlander
Hello all. Consider the following script:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
sDir = "C:\Test"
sDir = Replace(sDir, "\", "\\\\")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
  ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
    & "Targetinstance ISA 'Win32_SubDirectory' and " _
    & "TargetInstance.GroupComponent=" _
    & "'Win32_Directory.Name=""" & sDir & """'")
Do
  Set objLatestEvent = colMonitoredEvents.NextEvent
  sPartComponent = objLatestEvent.TargetInstance.PartComponent
  Wscript.Echo Now & vbCrlf & sPartComponent & vbCrlf
Loop
Set objWMIService = nothing
Set colMonitoredEvents = nothing

The script works fine for monitoring the single folder "C:\Test", but
I'm wondering if there's any way - using just one script - to monitor
multiple folders for any subfolders that get created? What if I wanted
to monitor all of the following folders simultaneously:

"C:\Test"
"C:\Test2"
"C:\Test3"
"C:\Test4"
"C:\Test5"

Any suggestions would be greatly appreciated.
Thanks!

- Dave

Author
14 Mar 2007 4:58 PM
urkec
I changed the script to monitor two folders and it worked. Problem is, if you
want to monitor more than just a few folders, the query will become to long.
I'm not sure if there's a better way of doing this, or how monitoring
multiple folders affects performance.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
sDir = "C:\test"
sDir1 = "C:\test1"
sDir = Replace(sDir, "\", "\\\\")
sDir1 = Replace(sDir1, "\", "\\\\")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE " _
& "Targetinstance ISA 'Win32_SubDirectory' and " _
& "(TargetInstance.GroupComponent=" _
& "'Win32_Directory.Name=""" & sDir & """' or " _
& "TargetInstance.GroupComponent=" _
& "'Win32_Directory.Name=""" & sDir1 & """')")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
sPartComponent = objLatestEvent.TargetInstance.PartComponent
Wscript.Echo Now & vbCrlf & sPartComponent & vbCrlf
Loop
Set objWMIService = nothing
Set colMonitoredEvents = nothing

--
urkec


Show quote
"Highlander" wrote:

> Hello all. Consider the following script:
>
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:" & _
>  "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> sDir = "C:\Test"
> sDir = Replace(sDir, "\", "\\\\")
> Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
>   ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
>     & "Targetinstance ISA 'Win32_SubDirectory' and " _
>     & "TargetInstance.GroupComponent=" _
>     & "'Win32_Directory.Name=""" & sDir & """'")
> Do
>   Set objLatestEvent = colMonitoredEvents.NextEvent
>   sPartComponent = objLatestEvent.TargetInstance.PartComponent
>   Wscript.Echo Now & vbCrlf & sPartComponent & vbCrlf
> Loop
> Set objWMIService = nothing
> Set colMonitoredEvents = nothing
>
> The script works fine for monitoring the single folder "C:\Test", but
> I'm wondering if there's any way - using just one script - to monitor
> multiple folders for any subfolders that get created? What if I wanted
> to monitor all of the following folders simultaneously:
>
> "C:\Test"
> "C:\Test2"
> "C:\Test3"
> "C:\Test4"
> "C:\Test5"
>
> Any suggestions would be greatly appreciated.
> Thanks!
>
> - Dave
>
>

AddThis Social Bookmark Button