\[Editor's Note: Email your scripting solutions (400 words or less) to Reader to Reader at [email protected] Please include your script and phone number. We edit submissions for style, grammar, and length. If we print your contribution, you receive $100.\]
Automatically Delete Old Files from Directories
Despite the decreasing cost of hard disk space, most Windows NT administrators still face the tedious task of cleaning up wasted space on servers. This task typically involves deleting temporary and old files that have already been backed up. I created the script DelOldFiles.vbs to automate this task. This script deletes files in the Temp directory and in a specified share that are older than the specified age limit, based on each file's last-modified date.
To delete old files in C:\temp and its subdirectories, you specify the age limit (in number of days) at the command line. For example, if you want to delete all files that are older than 7 days, you type
DelOldFiles.vbs C:\temp 7
at the command line. To delete old files in a specified share and its subdirectories, you specify the share's pathname and the age limit. For example, the command
DelOldFiles.vbs \\Server1\Share1 7
deletes all files in Share1 on Server1 that are older than 7 days. For tracking purposes, DelOldFiles.vbs generates a .log file that lists all the deleted files.
I wrote DelOldFiles.vbs to run on NT 4.0 machines running Service Pack 5 (SP5), SP6, or SP6a. You need Windows Script Host (WSH) 2.0 installed on the machines. You can download DelOldFiles.vbs from the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com) or from my Web site (http://www.scripthorizon.com). To use DelOldFiles.vbs, you need to make one modification to the script. Listing 1 shows the line you need to modify.
DelOldFiles.vbs permanently deletes files. Thus, before you use the script, you need to test it by running the script in a nonproduction environment. If you can't test it in a nonproduction environment, you can comment out the script's two delete statements and run the script in the production environment. You can then check the .log file, which will contain a list of files that would have been deleted.