Monitoring File Creation

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

At times, you might find it useful to know when a new file is added to the file system. For example, you might have a central repository for system administration scripts. Each time a new script is added to the repository, the script writer can be tasked with issuing an e-mail to other administrators. Alternatively, you can use a script to monitor the repository and automatically send an e-mail any time a new script is added. That way, administrators immediately know that a new script is available for use.

To keep track of newly created files, you need to write a script that includes two key elements. First, you need to monitor for new instances of the __InstanceCreationEvent class; new instances of this class are created each time a new managed object of any kind is created on the computer. Second, you need to limit data retrieval to new instances of the CIM_DirectoryContainsFile class.

CIM_DirectoryContainsFile is an association class that associates a specified folder with all the files contained within it. If you pass the name of a folder to CIM_DataContainsFile, it returns a collection of all the files in that folder. CIM_DataContainsFile has two components:

  • GroupComponent. The path to the folder being monitored
  • PartComponent. The individual files within that folder

Scripting Steps

Listing 11.29 contains a script that monitors the C:\Scripts folder and issues an alert whenever a new file is added to that folder. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.
  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  3. Use the ExecNotificationQuery method to monitor for new instances of the CIM_DirectoryContainsFile class, where the GroupComponent (the folder being monitored) is equal to "c:\\\\scripts". All four backslashes must be included in the query.
  4. Create a Do Loop with no exit condition. This causes the loop to continue (and the script to run) indefinitely. The only way to stop the loop is to terminate the script process.
  5. Create an object (objLatestEvent) to represent new instances of the CIM_DirectoryContainsFile class. Each time a new file is created in C:\Scripts, a new instance of objLatestEvent is created as well.
  6. Echo the PartComponent of the objLatestEvent.TargetInstance. In the association between CIM_DirectoryContainsFile and Win32_Directory, the PartComponent represents the path name of the newly created file.

Listing 11.29   Monitoring File Creation

1 2 3 4 5 6 7 8 9 10 11 12 
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:\\\\scripts""'") Do     Set objLatestEvent = colMonitoredEvents.NextEvent     Wscript.Echo objLatestEvent.TargetInstance.PartComponent Loop

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net