How To Automate Disk Cleanup and Defragmentation In Windows XP

Whenever we talk about maintaining Windows XP installation the most common concern is that the system gets slower by every passing day.  Answer to this very common problem is also very common which is defragment your hard disk regularly. I am not sure whether it really solves the problem but lets find a way to do it as easily as we can.

I will not get into details of fragmentation in this post as it is already explained in my other post here. Its is always a good idea to perform defragmentation after performing disk cleanup. This helps in two ways, first it will help us to remove all unwanted data on the hard disk (which might in turn make it perform better) and second it will reduce the time required to complete defragmentation.

I wanted to automate the whole process so that I don’t have to remember doing it.  It is very easy to perform the defragmentation without any intervention by invoking it through command line but that is not the case with clean up utility. The disk cleanup utility asks us to select what data to cleanup. So I started looking for a solution to execute cleanup manager without human intervention.

Cleanup Manager Configuration:

Cleanup manager is used to remove unwanted files (temporary internet files, temporary installation files, other temporary file, compression etc). To setup cleanup manager to run automatically we need to use following command:

cleanmgr /sageset:n

Where “n” can be any value from 0 to 65535. This index is used to choose between different configurations for cleanup manager when it is invoked to perform the actual cleanup activity. Lets assume that we are setting it to 1. This command will open a dialog box which will let you choose which files to delete as part of this configuration.

Cleanup Manager Configuration

Cleanup Manager Configuration

Whatever options you mark in this dialog will be examined when cleanup manager is executed with this configuration.

To run the cleanup manager with this configuration it can be invoked as:

cleanmgr /sagerun:n

Please note that there is no option to select the drive letter here, the cleanup will be performed on all local drives with chosen configuration.

Defragmenter  Configuration:

As it is defrag utility is capable of executing for command line without any intervention, I also enable verbose output to examine the log after the task is completed.

defrag c: -v > CDriveReport.txt

The above example shows command to defragment “C” drive and save the report in CDriveReport.txt.

Putting It All Together In A Batch File:

In order to automate these tasks we need to create a batch file which would look something like this:

@ REM Run the cleanup utility
cleanmgr /sagerun:1

@ REM Defrag C Drive
defrag c: -v > CDriveReport.txt

@ REM Defrag D Drive
defrag D: -v > DDriveReport.txt

You might have to add/delete lines for defrag command as applicable.

Creating A Schedule For This Activity:

Now we have everything required to create a schedule to perform this activity as given time automatically.

To create new scheduled task

  1. Go to Control Panel -> Scheduled Tasks -> Add Scheduled Task.
  2. This will open a wizard, just click next here.
  3. Now you will see list of application, but we don’t want any of them so click on browse and select the batch file we have created above and click next.
  4. In the next screen you need to select the frequency at which you want to execute this task. I would choose weekly. Click next.
  5. On the next screen select the time and day(s) as you wish and click next.
  6. The next screen will ask you for password, you can either enter the password here or change the settings to run this task only when you have logged later in advanced options. I would skip entering the password and click on next.
  7. On the next screen mark the checkbox to open advanced properties and click Finish. If you haven’t entered the password it might give you an warning, just click OK, we are going to set it right.
  8. This will open the advanced property screen as shown below. Mark the checkbox marked “Run only if logged on”
  9. You can also create multiple schedules in Schedule tab.
Scheduled Task Settings

Scheduled Task Settings

You can get the defragmentation logs in the same folder as your batch file.

That’s about it, you can relax now as you don’t have to worry about cleaning up and defragmenting your hard disk any more.


Please note that everything you have asked to cleanup will be cleaned up and there is no way to get that data back. So be careful about what you have chosen during cleanup manager configuration.

Links Related to This Post:

This entry was posted in How To, System Tools, Windows Software and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s