PDA

View Full Version : Script to notify upon file creation (W2K)



smarttmelanie
03-31-2004, 03:49 PM
When I run the script below, it will tell me if a file has been created in the C: drive called 'Inspections.txt'. What I need is a script that will notify me if a file (without an extension) is created called 'Inspection' and for it to notify me if it has been created anywhere in the C: drive not just the root. PLEASE HELP...MY JOB DEPENDS ON IT

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
x = Instr(objLatestEvent.TargetInstance.PartComponent,"Inspections.txt")
If x <> 0 Then
Wscript.Echo objLatestEvent.TargetInstance.PartComponent
End If
Loop

erickwisler
04-17-2004, 07:07 AM
Here is a modified version of your script. If the Upper and Lower cases are of importance just leave out the ucase() fonction.

' 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:\\\\test""'"

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

NextEvent.TargetInstance.PartComponent

smarttmelanie
04-26-2004, 10:40 AM
Thanks for the response. I cut and paste your script into notepad, renamed it .vbs, ran it, then created a file called new.txt in the C:\test folder and nothing happened. Am I missing something?

Also, I was wondering if the script would notify me if a file is created within the ENTIRE c:\ if a file called 'Inspection' is created.....(there is no file extension associated with this file).
I hope this helps and i REALLY appreciate your help!!