Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Monitor Folder Recursively using WMI

Reply
Thread Tools Display Modes

Monitor Folder Recursively using WMI

 
 
Joe Pang
Guest
Posts: n/a

 
      11-20-2008
I am writing a VBscript to monitor a folder for file creation and deletion.
This is a script that I copied from Microsoft Scripting Guy to start with.
It works well except that it only monitors file activities on the root of
the folder. I would like it to recursively monitor sub-folders as well.

Any suggestions?





strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\My_Folder""'")

Do While TRUE
Set objEventObject = colMonitoredEvents.NextEvent()

Select Case objEventObject.Path_.Class
Case "__InstanceCreationEvent"
Wscript.Echo "A new file was just created: " & _
objEventObject.TargetInstance.PartComponent
Case "__InstanceDeletionEvent"
Wscript.Echo "A file was just deleted: " & _
objEventObject.TargetInstance.PartComponent
End Select
Loop



 
Reply With Quote
 
 
 
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      11-20-2008

"Joe Pang" <> wrote in message
news:...
>I am writing a VBscript to monitor a folder for file creation and deletion.
>This is a script that I copied from Microsoft Scripting Guy to start with.
>It works well except that it only monitors file activities on the root of
>the folder. I would like it to recursively monitor sub-folders as well.
>
> Any suggestions?
>
>
>
>
>
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
>
> Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
> ("SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE " _
> & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
> & "TargetInstance.GroupComponent= " _
> & "'Win32_Directory.Name=""c:\\\\My_Folder""'")
>
> Do While TRUE
> Set objEventObject = colMonitoredEvents.NextEvent()
>
> Select Case objEventObject.Path_.Class
> Case "__InstanceCreationEvent"
> Wscript.Echo "A new file was just created: " & _
> objEventObject.TargetInstance.PartComponent
> Case "__InstanceDeletionEvent"
> Wscript.Echo "A file was just deleted: " & _
> objEventObject.TargetInstance.PartComponent
> End Select
> Loop


AFAIK you have to monitor each folder separately. Furthermore the process is
quite CPU-intensive, like many WMI processes. Have a look at your CPU
loading in the Task Manager. You'll probably see a substantial spike once
every 10 seconds.


 
Reply With Quote
 
Joe Pang
Guest
Posts: n/a

 
      11-21-2008
I came across a few commerical software/shareware that can do the job -
monitor a folder and its sub-folders recursively and executive a program on
changes. I was tempted to buy one but none of them really works well. That
was why I was tempted to create my own script. Since they can do it, I
thought I could do it using WMI.

I know it will cause some spike on CPU but so far I don't see any
significant impact.




"Pegasus (MVP)" <> wrote in message
news:...
>
> "Joe Pang" <> wrote in message
> news:...
>>I am writing a VBscript to monitor a folder for file creation and
>>deletion. This is a script that I copied from Microsoft Scripting Guy to
>>start with. It works well except that it only monitors file activities on
>>the root of the folder. I would like it to recursively monitor
>>sub-folders as well.
>>
>> Any suggestions?
>>
>>
>>
>>
>>
>> strComputer = "."
>> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
>> "\root\cimv2")
>>
>> Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
>> ("SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE " _
>> & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
>> & "TargetInstance.GroupComponent= " _
>> & "'Win32_Directory.Name=""c:\\\\My_Folder""'")
>>
>> Do While TRUE
>> Set objEventObject = colMonitoredEvents.NextEvent()
>>
>> Select Case objEventObject.Path_.Class
>> Case "__InstanceCreationEvent"
>> Wscript.Echo "A new file was just created: " & _
>> objEventObject.TargetInstance.PartComponent
>> Case "__InstanceDeletionEvent"
>> Wscript.Echo "A file was just deleted: " & _
>> objEventObject.TargetInstance.PartComponent
>> End Select
>> Loop

>
> AFAIK you have to monitor each folder separately. Furthermore the process
> is quite CPU-intensive, like many WMI processes. Have a look at your CPU
> loading in the Task Manager. You'll probably see a substantial spike once
> every 10 seconds.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing folder permissions recursively/ BlackShuck Windows Vista Security 3 12-12-2007 06:52 AM
Re: Folder Redirection creates recursively nested folders Lanwench [MVP - Exchange] Windows Server 0 12-10-2006 01:29 PM
Re: Recursively list all subfolders Andrew Watt [MVP] Scripting 0 11-15-2006 04:30 PM
Re: Recursively list all subfolders Les Scripting 0 11-15-2006 04:27 PM
PCMCIA interrupt: ISR run recursively Panda Ng Windows Vista Drivers 3 06-03-2004 01:01 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59