Monitoring and Archiving Newly Created Files

August 13th, 2007
1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 4.43 out of 5)
Loading ... Loading ...

I’ve received an interesting question about how to archive new files every day in an office setting. The solution that I’ve come up for this reader involves the creation of an event-driven script that can monitor a folder for newly created files and copy them to another location.

Hi, I need help copying newly created files using WSH. Every day in my office new files are being created. I want copy those files into my server. […] How do I do this? Please help me, sir.
- Ramana R.

My first thoughts after reading this were that there are a couple of ways to tackle this. First, I could create a simple script to run daily as a Scheduled Task that would check the file creation dates for a folder and copy any files created within 24 hours. Then, I thought, “why not have some fun with it?” So, I decided that this would be much better as an event-driven script.

Event-driven scripting is a little known scripting technique that is based on event triggers. The script runs in the background–much like a service–and reacts each time a specific event occurs. (In this case, a file creation.)

Essentially, the script I’ve written watches a specified folder and waits until a new file has been created. When the event is triggered, it copies the file to another archive location. Of course, I’ve included all of the bells and whistles including some error-handling and logging.

To begin, you need to include a few details for the script. It needs to know what directory to watch (strSource) and where to copy files (strDest). Once you define a source, the script needs to know whether or not it should overwrite files if there is a naming conflict.

strSource = "C:\test"
strDest = "C:\test2"
bOVERWRITE = vbTrue

This script is designed to be run on a local machine, so the source path should be a full local path (or a mapped network drive). The destination, however, doesn’t have to be. If you want to archive files to another machine, such as a server, provide a fully qualified UNC path and be sure that the currently logged on user has write permission for that share.

Next we begin building the script. Since this script uses events provided by WMI, we’ll need to add a reference to that next.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")

The next step is to write the WMI query that will provide our script functionality. Even those of you with some WMI scripting experience may find this query a bit complex. I’ll try to break it down a bit to make it easier. For a more thorough explanation, check out my articles on ASP Free.

Event Scripting with WMI
More Event Scripting with WMI

Pages: 1 2 3 4

Please use the trackback link when linking to this post.

Related Posts:

Add to Technorati Favorites

Leave a Reply