PDA

View Full Version : Look in all subfolders (All)



smarttmelanie
04-28-2004, 08:46 AM
The script below will notify me if a file called 'Inspection' was created in C:\. However, it doesn't notify me if the file (Inspection) is created anywhere else except the root. Is there a way I can specify that it notify me if the file is created in all subfolders of C:\?

' Script that will detect the creation of a specific file in C:\test


Dim strQuery, FileToCheck

strQuery = "Select * From __InstanceCreationEvent Within 2 " _
& "Where TargetInstance ISA 'cim_DirectoryContainsFile' " _
& "AND TargetInstance.GroupComponent=" _
& "'Win32_Directory.Name=""c:\\\\""'"

FileToCheck = "Inspection"

' for remote, define a sComputerName variable and exchange the line
' above with this two ( in addition to the changes in the
' "GetObject("winmgmts:")" statement as well):
' & "AND TargetInstance.GroupComponent='\\\\" & sComputerName _
' & "\\root\\cimv2:Win32_Directory.Name=""c:\\\\test""'"

Set oEvents = GetObject("winmgmts:").ExecNotificationQuery (strQuery)

' now wait for events ...
Do While True ' loop indefinitely
Set oNTEvent = oEvents.NextEvent.TargetInstance ' retrieve event
if instrRev( ucase(oNTEvent.PartComponent), "\" & ucase(FileToCheck) ) = _
( len(oNTEvent.PartComponent) - len("\" & FileToCheck) ) then _
WScript.Echo "message " & oNTEvent.PartComponent
Loop

' Will give you an output on this format for each new file:
' \\\root\cimv2:CIM_DataFile.Name="c:\\test\\New.txt"
wscript.quit




Thanks for any feedback!!!

dineshcooper
05-04-2004, 02:18 AM
Hi Melanie

I found your post on every wmi/scripting forum
out there.

I was looking for a script that also monitors the entire drive but i couldn't find much and I then decided to do it in c# as a windows service.

Not sure if that helps at all but if you really need to do it with a script then try to email the scripting guys at scripter@microsoft.com, if anyone knows the answer to your question it should be them.

They do say on the microsoft scripting site:

You can use the WMI event monitoring capabilities to monitor changes to the file system. If you choose to carry out such monitoring, it is highly recommended that you limit your monitoring to a specific file or folder. As noted previously, file systems are in a constant state of flux; attempting to monitor the entire file system on a computer is very slow, very processor intensive, and results in a huge quantity of data, most of which is of no interest to you. By limiting your monitoring to a specific file or folder, you can ensure that you are immediately notified of changes to the file system and that you are notified only of the changes that really matter to you.

So it can be done then
Cheers

<P ID="edit"><FONT class="small">Edited by dineshcooper on 05/04/04 01:27.</FONT></P>