In "Automatically Delete Old Files", I discuss DelFile.vbs, a script that you can use to delete files based on their last modified date. This script works well in deleting files in specific folders. However, my administrators wanted an additional tool to delete entire folders that were no longer used. So, I wrote the DelFolder.vbs script. DelFolder.vbs loops through all the folders in the specified root directory and deletes folders (and their contents, including subfolders) based on their last modified date. This script is useful for deleting folders that users create, then forget about. In our organization, this situation often occurs on network drives.
To use DelFolder.vbs, you need to run the script using the CScript host. The command syntax is
delfolder.vbs drive days_since_last_modified delete(0)_or_display(1)
where drive is the root directory you want to check. You use the days_since_last_modified argument to specify the number of days you'll allow since the last modified date. You use the last parameter to indicate whether you want to just display the folders (specify a value of 1, which is the default) or actually delete the folders (specify a value of 0). For example, if you want to preview folders on the D drive that haven’t been modified for at least 14 days, you'd use the command
delfolder.vbs D:\ 14 1
To delete those folders, you'd change the last parameter to 0:
delfolder.vbs D:\ 14 0
Listing 1 shows DelFolder.vbs. The script begins by making sure that three arguments were provided on the command line when the script was launched. If there aren't three arguments, the script displays the launch syntax and quits. If there are three arguments, the script creates an instance of the FileSystemObject object, then calls VBScript's DateAdd function, which callout A shows. The DateAdd function returns a date to which a specified time period has been added. In this case, it adds the number of days specified on the command line to the current date, which VBScript's Date function retrieves. The result is assigned to the olddate variable.
As callout B in Listing 1 shows, the script uses Windows Management Instrumentation (WMI) to get the folders in the root directory specified on the command line. This code uses VBScript's GetObject function to connect to the WMI service on the local computer. GetObject returns a reference to WMI's SWbemServices object, which represents an authenticated connection to WMI on that computer. The code uses the SWbemServices object's ExecQuery method to make a WMI Query Language (WQL) query. The query uses the Associators Of keyword to r etrieve all folders and subfolders in the root directory specified on the command line. (For information about how the Associators Of keyword works, go to the ASSOCIATORS OF Statement Web page.) The retrieved folders are assigned to the colSubfolders variable.
For each folder in the colSubfolders variable, DelFolder.vbs calls the ShowSubFolders subroutine. ShowSubFolders uses the FileSystemObject object's DateLastModified property to retrieve the date in which the folder was last modified. When the last modified date is older than the date in the olddate variable, the script either displays that folder (if the last command-line parameter was 1) or deletes it (if the last command-line parameter was 0).
Share Your Scripting Experiences
Share your scripting discoveries, comments, solutions to problems, and experiences with products. Email your contributions to firstname.lastname@example.org. 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.