Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / Win32
Tip/Trick

Custom disk clean up using WSH VBScript

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
1 Aug 2014CPOL2 min read 17.6K   324   1   1
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.Image 1

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.

Image 2

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.

Image 3

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.

Image 4

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.

VB
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.

VB
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.

VB
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)


Written By
Software Developer (Senior)
India India
Gautham Prabhu K is a Software Engineer with 10+ 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

 
GeneralMy vote of 4 Pin
nd94765654727-Jun-16 9:27
nd94765654727-Jun-16 9:27 
thank you for a simple solution.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.