How Can I Automatically Run a Script Any Time a File is Added to a Folder?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is it possible to have a script automatically run any time a file is added to a specific folder?

— MB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, WM. Yes, this is possible, thanks to the magic of WMI events, which allow you to write a script to monitor for something of interest (like a file being added to a folder) and then take some action any time an event like that occurs. We don’t have time to discuss WMI events in any detail in this column, but they can be incredibly useful to script writers. Consequently, it might be worth your while to check out this Tales from the Script column.


In the meantime, here’s a script that monitors the C:\Scripts folder. Any time a file is added to this folder, the script responds by echoing the name of that new file:

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

Again, there’s too much going on here to go through the script step-by-step, but what we are looking for are instances of the __InstanceCreationEvent class; instances of this class are automatically created any time a new managed object (that is, something WMI knows about) is created on a computer. Although the query is a little complicated, it boils down to this: we want to be alerted to any new items that show up in C:\Scripts. (Note that C:\\\\Scripts is not a misprint; you really need all four slashes here.)


We also need to mention that a WMI script like this works by “polling;” it periodically goes out and checks to see if any new files have been added to the folder. For this sample, we’re checking every 10 seconds (that’s what the WITHIN 10 represents) to see if there are any new files in the folder. If that’s too fast or too slow, you can change that value to anything you want. Keep in mind two things, however. For one, if you poll too often (say, every second), you’ll have a script that is constantly running, and could theoretically put a drain on your system resources.


Conversely, if you make the value too long, you might miss new files, assuming they get added and then deleted before the polling time expires. For example, say your script checks every 5 minutes for new files. If you add 100 new files and then delete all those files 3 minutes later, the script will never know that those files were added to the folder. That’s because scripts like this work by comparing the files that currently in the folder with the files that were in the folder the last time the script checked. Play around with the polling interval and see what works best for you.


As we noted, this script simply echoes the name of any new file added to C:\Scripts. What if you wanted to do something a little fancier any time a file is added to the folder? No problem; just replace this line of code with the code you want executed any time a new file is detected:

Wscript.Echo objLatestEvent.TargetInstance.PartComponent

By the way, would you prefer to be notified any time a file is deleted from C:\Scripts? That’s easy enough; just monitor C:\Scripts for new instances of the __InstanceDeletionEvent class:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & _
strComputer & “\root\cimv2”)
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
(“SELECT * FROM __InstanceDeletionEvent 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

Hey, we told you WMI events were worth knowing about!