Click here to Skip to main content
Click here to Skip to main content

Custom disk clean up using WSH VBScript

, 1 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Perform custom disk clean up when hard disk free space goes low

Introduction

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.

Background

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

	 Case 3
   
		 '+++++++++++++++++++++++++++++++++++++++
		 'Convert drive letter to upper case.
		 '+++++++++++++++++++++++++++++++++++++++

		  strDriveToMonitor = UCase (Wscript.Arguments(0))

		  strFreeSpaceNeeded = Wscript.Arguments(1) 
		  strBatchFileToRun =  Wscript.Arguments(2) 		

	 Case Else

    		'++++++++++++++++++++++++++++++
		    ' Call the display syntax help routine.
		'++++++++++++++++++++++++++++++

	  	Syntax

End Select

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 
       		
       		 '+++++++++++++++++++
        	 'Check for the specified free space in GB
       		 ' objItem.FreeSpace retuns the free disk space in bytes.
       		 '+++++++++++++++++++        

                 intFreeSpaceinGB = Int( 0.5 + ( objItem.FreeSpace / 1073741824 ))     

        	 If ( intFreeSpaceinGB  <= Int(strFreeSpaceNeeded)) Then

           	    '+++++++++++++++++++
  		    'Run the batch file
             	    '+++++++++++++++++++

            	    createobject("wscript.shell").Run strBatchFileToRun

        	 End If

	End If
Next 

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gautham Prabhu K
Software Developer (Senior)
India India
Gautham Prabhu K is a Software Engineer with 8+ years of experience in designing and developing software solution. He has experience working in Payment Gateway, Process Automation and Investment Banking industries.
 
Gautham has passion for engineering software solutions. He likes to design and build them efficiently, he has primarily worked on Microsoft .NET technology.
 
Gautham has dream of building his own software company someday. During his free time he likes to watch movies, go on long drives and write technical blogs/article.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150224.1 | Last Updated 1 Aug 2014
Article Copyright 2014 by Gautham Prabhu K
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid