I had to maintain a production server where I had to clean up temporary files when hard disk free space went below certain GB to maintain optimal performance of my server. The temporary files location and type can vary over time. So I decided to use WSH VBScript to determine hard disk space and then ask it run a custom batch file when free space in disk went below certain GB. The WSH script will be run perodically using windows task scheduler.
I assume that reader of this article know some basics of WSH and batch file. There may be many other ways to acheive disk clean up. I have chosen WSH as it easier to access windows system resources with it, creating batch file for performing custom clean up actions is easier to maintain. The example used in article is given with respect to Windows7 OS.
Using the Script
To use my script unzip the file given in article above and follow steps mentioned below:
Step1 Copy script to a folder
Take the CleanUpDisk.vbs file and place it a folder lets say C:\CleanUpDisk.
Step2 Create batch file and test cleanup operation
Create a batch file (Custom.bat) to do clean up operation. Test the script and batch by typing command in command line as shown below.
The above parameters checks if free disk space of C: drive is less than or equal to 15 GB then execute batch file Custom.bat.
Step3 Create scheduled task with proper parameters
Use Windows Task Scheduler to schedule a periodic task to execute script at periodic intervals as required by user. The task should run with administrator privilege to run smoothly by checking in Run with highest privileges option as shown below.
In Actions tab of Task Scheduler specify Add arguments (optional) option with drive you want to monitor, disk free space size in GB and batch file name to execute. In Start in (optional) specify the location where batch file is located.
How Script works
The script checks for command line parameters.
If parameter count is proper then extract drive name and batch file path; else show syntax help to user.
Select Case WScript.Arguments.Count
strDriveToMonitor = UCase (Wscript.Arguments(0))
strFreeSpaceNeeded = Wscript.Arguments(1)
strBatchFileToRun = Wscript.Arguments(2)
Next get the WMI service object and query computer for all available logical disk in machine.
Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DriveType=3",,48)
Loop through the collection till disk name matches command line parameter then determine free space size of disk and execute batch file.
For Each objItem in colItems
If objItem.Name = strDriveToMonitor Then
intFreeSpaceinGB = Int( 0.5 + ( objItem.FreeSpace / 1073741824 ))
If ( intFreeSpaceinGB <= Int(strFreeSpaceNeeded)) Then
Points of Interest
- Make sure script has proper rights to execute clean up batch file while creating task in Task Scheduler.
- For learning more on writing batch file refer this link.
- The CleanUpDisk.vbs script file I created has commented debug messages code which can be uncommented to debug the script for future modification.