Click here to Skip to main content
12,756,892 members (31,346 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 1 Aug 2014

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


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

	 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.


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("").Run strBatchFileToRun

        	 End If

	End If

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.


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


About the Author

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

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
nd94765654727-Jun-16 10:27
membernd94765654727-Jun-16 10:27 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 1 Aug 2014
Article Copyright 2014 by Gautham Prabhu K
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid