Subroutine Makes File Cleanup a Snap

Use the CleanUpOldFiles subroutine in any script with which you want to quickly delete files.

Readers

February 27, 2007

3 Min Read
ITPro Today logo in a gray background | ITPro Today


I developed the CleanUpOldFiles subroutine to use in scripts. This subroutine deletes all files older than a specified age in a specified directory. The CleanUpOldFiles subroutine's two parameters specify the age and directory:
• intDeleteAge—an integer that specifies the age threshold (in days). All files older than the specified age will be logged and deleted. For example, if you specify 30, all files older than 30 days (based on the current date) will be logged and deleted.
• strDirectory—a string that specifies the directory or Universal Naming Convention (UNC) path from which to delete the files. Note that if the path contains spaces, you must enclose the path in double quotes (").

Listing 1 shows CleanUpOldFiles. This subroutine logs all its actions in the log file specified by the fsoErrorLog variable. This variable is a FileSystemObject object that you need to create elsewhere in your script. You can find a .vbs file containing the CleanUpOldFiles code in the 94749.zip file, which you can download by clicking the "Download the Code Here" link. This .zip file also includes all the other code mentioned here.

The CleanUpOldFiles subroutine uses another subroutine called HandleError for generic error handling. The HandleError subroutine, which Listing 2 shows, handles generic script cleanup and emails the log file to the specified recipients. For script cleanup, the HandleError subroutine calls on a third subroutine named CleanUp. For emailing, the HandleError subroutine uses a free utility named Blat.exe, which you can download from SourceForge.net (http://sourceforge.net). Blat needs to be in the system directory and needs to be configured before a script can successfully use it. If you don't want to use Blat and the HandleError subroutine, you can use a version of the CleanUpOldFiles subroutine that only deletes the files and doesn't log the results. You can find this modified subroutine, named CleanUpOldFiles_No_Logging.vbs, in the 94749.zip file.

The Delete-Old-Files.vbs script in Listing 3 demonstrates how to use the CleanUpOldFiles subroutine. This simple script deletes files from the specified directory. (Typically, my scripts are more complex because I need to keep backup copies of files that I’ve processed as well as keep logs of those scripts' executions.) Callout A in Listing 3 shows the code that creates the fsoErrorLog variable. Callout B in Listing 3 shows how to call the CleanUpOldFiles subroutine. At the end of the script, you'll find the code for the CleanUpOldFiles, HandleError, and CleanUp subroutines. You'll also find the code for the HelpListing subroutine, which provides help information for launching the script.

I use the CleanUpOldFiles subroutine in all my scripts to quickly and easily delete files anywhere on my clients’ networks. You should give it a try, too.

—Buzzy Winter

Share Your Scripting Experiences


Share your scripting discoveries, comments, solutions to problems, and experiences with products. Email your contributions to [email protected]. Please include your full name and phone number. We edit submissions for style, grammar, and length. If we print your submission, you’ll get $100.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like