Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Monitor Folder Recursively using WMI

HTVi
TV Discussion Newsgroups

Monitor Folder Recursively using WMI
"Joe Pang" <pangjo[ at ]hotmail.com> 11/20/2008 9:45:27 PM
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



Re: Monitor Folder Recursively using WMI
"Pegasus \(MVP\)" <I.can[ at ]fly.com.oz> 11/20/2008 10:14:09 PM

"Joe Pang" <pangjo[ at ]hotmail.com> wrote in message
news:eiyVOl1SJHA.6000[ at ]TK2MSFTNGP02.phx.gbl...
[Quoted Text]
>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.


Re: Monitor Folder Recursively using WMI
"Joe Pang" <pangjoe[ at ]hotmail.com> 11/21/2008 3:31:14 AM
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)" <I.can[ at ]fly.com.oz> wrote in message
news:uir4Q11SJHA.408[ at ]TK2MSFTNGP02.phx.gbl...
[Quoted Text]
>
> "Joe Pang" <pangjo[ at ]hotmail.com> wrote in message
> news:eiyVOl1SJHA.6000[ at ]TK2MSFTNGP02.phx.gbl...
>>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.
>


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen