Software

Regularly scheduled hard disk maintenance for your XP users

Disk Cleanup and Disk Defragmenter are two important hard disk maintenance programs. The trick is to get users to run them. Greg Shultz has the solution: Scheduled Tasks and a VBS script.


One of the best ways to keep any Windows system running in tip-top shape is to regularly take care of the hard disk. Windows XP comes with two utilities designed to keep the hard disk running smoothly—Disk Cleanup and Disk Defragmenter. Getting users to run either of these utilities on a regular basis isn’t very easy, though. As a result, hard disks can become sluggish and ultimately be the source of poor system performance. You can prevent this situation by configuring Scheduled Tasks to run Disk Cleanup and Disk Defragmenter on a regular basis.

I’ll give you a brief overview of Windows XP’s Disk Cleanup and Disk Defragmenter and show you how to configure Scheduled Tasks to run these utilities. I’ll conclude with a VBS script you can use to distribute the tasks created by Scheduled Tasks to all the Windows XP Professional systems on your network.

The Disk Cleanup process
Disk Cleanup is designed to free up space on your hard disk by removing unnecessary files that can accumulate on your hard disk during normal use. As shown in Table A, Disk Cleanup categorizes these unnecessary file types.
Table A

File Category                          

Description

Catalog Files for Content Indexer

Files left over from a previous Indexing Service indexing operation

Debug Dump Files

Troubleshooting information files created by Windows during a system crash

Downloaded Program Files

ActiveX controls and Java applets that are downloaded from certain Web sites and temporarily stored in the Downloaded Program Files folder

Offline Files

Local copies of files that exist on a network drive that a user makes available via the Offline Files tool

Offline Web Pages

Local copies of Web pages that a user saved via his or her browser

Old Chkdsk Files

Files created by the Chkdsk command that contain lost file fragments

Recycle Bin

Files a user deleted from the hard disk

Setup Log Files

Log files created during the installation of an application

Temporary Files

Temporary files used by various applications

Temporary Internet Files

A cache of Web pages stored on the user's hard disk in order to provide quick viewing

Temporary Offline Files

Copies of recently used network files that are automatically cached by Windows

Temporary Setup Files

Files created during the installation of an application

Temporary Remote Desktop Files

Image files created by Remote Desktop to improve performance

WebClient/Publisher Temporary Files

A cache of files stored on a user's hard disk used by the WebClient/Publisher service to improve performance

Disk Cleanup is designed to remove unnecessary files from your hard disk.

When you launch Disk Cleanup from the Start menu, you’ll be prompted to select a drive letter. You’ll then see a dialog box in which you can choose the file types you want to remove from the hard disk. You’ll have to perform this configuration procedure each time you manually launch Disk Cleanup.

You can automate the Disk Cleanup configuration procedure by saving your settings to a file. Once you do that, you can configure the Scheduled Tasks utility to run Disk Cleanup without any manual intervention.

Saving Disk Cleanup configuration settings
The steps for saving Disk Cleanup configuration settings don’t appear in any of the Windows XP documentation. Now, considering the various types of file types that Disk Cleanup deletes, you may not want to delete all of the files all of the time. For example, you might want to delete Temporary Internet Files once a week and empty the Recycle Bin once a month. To do so, you can create any number of Disk Cleanup configuration settings files—one for each type of Disk Cleanup operation you want to perform.

To begin, access the Run dialog box by selecting Start | Run or pressing [Windows]R. Then, in the Open text box, type this command:
Cleanmgr /d x: /sageset:n

In this command line, Cleanmgr is Disk Cleanup’s executable file name, /d x:is the letter of the drive you want to clean, /sageset is a special configuration command that tells Disk Cleanup to save the settings in the registry, and n is a unique number from 0 to 65,535 that is used to designate a unique configuration settings file. For example, you could create your first configuration settings file for drive C: by typing Cleanmgr /d C: /sageset:1 in the Open text box.

When you do so, you’ll see a Disk Cleanup Settings dialog box like the one shown in Figure A. You can then select the check boxes next to the categories of files that you’d like Disk Cleanup to remove from your hard disk.

Figure A
Use the Disk Cleanup Settings dialog box to select the files you want to remove from your hard disk.


When you finish selecting check boxes, click OK. Disk Cleanup will save the settings in the registry. If you want to create additional Disk Cleanup configurations, just repeat the steps and use the commands Cleanmgr /d C: /sageset:2,Cleanmgr /d D: /sageset:3, and so on.

Scheduling Disk Cleanup sessions
Once you create Disk Cleanup configuration files, you’ll want to add them to the Scheduled Tasks utility and then schedule them to run on a regular basis. To get started, click Start | All Programs | Accessories | System Tools | Scheduled Tasks. In the Scheduled Tasks window, double-click the Add Scheduled Task icon to start the Scheduled Task Wizard.

On the wizard’s initial screen, peruse the information presented and click Next. When you’re asked to choose the program that you want to run, scroll through the list and select Disk Cleanup. On the next screen, you’ll be prompted to assign the task an appropriate name and select a regular time interval for the task to be run. To complete the schedule, on the next screen choose the day of the week and the time that you want the task to run.

Next, enter the username and password of a user. Scheduled Tasks will then run this task as if this user started it. If you plan on distributing this task to all the users on your network, as I’ll explain in a moment, enter the username and password of an administrator account that is recognized and has privileges to run on all of the systems on the network.

When you click Next, you’ll see the last screen in the wizard, which basically asks you to confirm the new task’s schedule. If everything looks correct, select the Open Advanced Properties… check box and click the Finish button.

After a moment, you’ll see the Disk Cleanup properties dialog box. Notice that the Run text box contains the path and executable file name for Disk Cleanup:
C:\Windows\System32\Cleanmgr.exe

Now, to have Disk Cleanup use the configuration settings that you created earlier, add the special parameter sagerun:n to the end of the command line, as shown in Figure B.

Figure B
To configure Disk Cleanup to use your custom configuration settings, add a special parameter to the end of the command line.


To complete the operation, click OK. If you created other Disk Cleanup configurations, repeat the steps above to add them to the Scheduled Tasks utility.

The Disk Defragmenter process
When you launch Disk Defragmenter from the Windows XP Systems Tools menu, you’ll see a Microsoft Management Console window where you can select a drive letter and choose either an Analyze or Defragment operation. The Analyze operation is designed to give you an idea of how fragmented the hard disk is and then, based on that information, recommend whether you need to defragment the hard disk. Performing all these steps manually can be a hassle.

Note
Keep in mind that your hard disk must have at least 15 percent free space for Disk Defragmenter to completely defragment the hard disk. Disk Defragmenter needs at least this amount of free space as a sorting area for file fragments. If a hard disk has less than 15 percent available free space, Disk Defragmenter will only partially defragment the hard disk. It’s a good idea to run Disk Cleanup before you run Disk Defragmenter.

Running Disk Defragmenter from the command line
You can automate the Disk Defragmenter procedure by running this utility from the command line and configuring its operation via special parameters. Once you know how to do so, you can configure the Scheduled Tasks utility to run Disk Defragmenter without any manual intervention.

To run Disk Defragmenter from the command line, open a Command Prompt window and type the following command, where x is the drive letter of the hard disk you want to defragment and /parameter is one of three special command line parameters that configure how you want Disk Defragmenter to run:
Defrag x: [/parameter]

The three parameters are described as follows:
  • /a - Analyzes the volume and displays a summary of the analysis report.
  • /v - Displays the complete analysis and defragmentation reports; can be used in combination with /a to display only the analysis report.
  • /f - Forces defragmentation of the volume regardless of whether it needs to be defragmented.

When you use either the /a or /v parameters, Disk Defragmenter will display the results on the command line. If you’d rather have the results available as a file, you can use the DOS redirection symbol to send the report to a file. For example, you can redirect the results to a file called DefragReport.txt with this command:
Defrag x: [/parameter] > DefragReport.txt

Scheduling your Disk Defragmenter sessions
Scheduling Disk Defragmenter is pretty straightforward once you know how to run the utility from the command prompt. To begin, launch the Scheduled Tasks utility as described earlier. When you see the Scheduled Tasks window, double-click the Add Scheduled Task icon to start the Scheduled Task Wizard. Then, when you’re prompted to select a program, click the Browse button, open the C:\Windows\System32 folder, select the Defrag.exe file, and click Open.

You’ll then follow through the wizard and configure the schedule as described earlier. Again, when you get to the last screen in the wizard, select the Open Advanced Properties… check box and click the Finish button.

Next, you’ll see the Disk Defragmenter properties dialog box. Add the special configuration parameters to the end of the command line in the Run text box.

To completely automate the Defragment operation, I suggest that you use only the /f parameter. For example, to defragment drive C:, you’d add the parameters to the command line in the Run text box, as shown in Figure C.

Figure C
To completely automate Disk Defragmenter, use the /f parameter to force a full defragmentation operation.


Distributing Scheduled Tasks
Let’s take a look at the steps involved in configuring the mechanism for distributing tasks to all the systems on your network. To begin, you’ll create a shared folder on your system that will serve as a central distribution point for the tasks. You’ll copy the tasks that you want to distribute from the Scheduled Tasks folder to your distribution folder. You’ll then create a small Windows Script Host file in VBScript, along with a specially configured shortcut that will function as the distribution tool. Finally, you’ll attach the shortcut to an e-mail message and send it to the appropriate users.

Creating the central distribution folder
Creating the central distribution folder is easy: Just create and share the folder as you normally would. The only requirement is that the system containing the central distribution folder be one that is always on so that all users can access it at any time.

Copying the tasks
Once you’ve created your central distribution folder, you can copy the tasks from the Scheduled Tasks folder to it. To begin, launch Windows Explorer and open the C:\Windows\Tasks folder. Right-click on the Disk Cleanup and Disk Defragmenter tasks and drag them from the Tasks folder to the central distribution folder. When you drop the task, select the Copy Here command from the shortcut menu. Once you copy the task to the central distribution folder, you’ll notice that the extension JOB is attached to the file and the task’s icon changes—the clock is replaced by a red circle with an X in it, as shown in Figure D. While this icon indicates that the task is disabled, this is nothing to be concerned about because once the task is copied to a user’s Task folder, it will be properly configured once again.

Figure D
When you copy a task to the central distribution folder, you’ll notice that the task’s icon changes to a red circle with an X in it.


Exporting the Disk Cleanup settings
Since Disk Cleanup stores its configuration settings in the registry, you’ll need to export those settings so that you can send them out along with the Disk Cleanup task; you’ll use the Registry Editor to do so. Be aware that incorrectly editing the registry may severely damage your system.

Launch the Registry Editor by accessing the Run dialog box and typing Regedit.exe in the Open text box. Once the Registry Editor is open, access the HKEY_LOCAL_MACHINE key and drill down through the following subkeys:
  • Software
  • Microsoft
  • Windows
  • CurrentVersion
  • Explorer
  • VolumeCaches

When you get to VolumeCaches, you’ll see that it contains subkeys for each of the categories listed in the Disk Cleanup Settings dialog box. You can find your Disk Clean configuration settings in each of these subkeys stored as a DWord binary entry named StateFlags#, where # is the number you used along with the /sageset configuration command.

To export the Disk Clean configuration settings to a registry hive file, right-click on VolumeCaches and select the Export command. When you see the Export Registry File dialog box, locate your distribution folder and save the file as DiskCleanup.reg.

Creating the script file
The script file that will take care of distributing the task is shown in Listing A. Simply open Notepad, type each line as shown, and save the file as Distribution.vbs. As you do, replace \\ComputerName\ShareName with the UNC path to your shared distribution folder. If typing in lines of code isn’t your cup of tea, don’t worry; the Distribution.vbs script is available for download.

Listing A: The distribution script
  • Dim WshShell, WshNetwork, FileSystem
  • Set WshShell = WScript.CreateObject("WScript.Shell")
  • Set WshNetwork = WScript.CreateObject("WScript.Network")
  • Set FileSystem = CreateObject("Scripting.FileSystemObject")
  • WshNetwork.MapNetworkDrive "W:", "\\ComputerName\ShareName"
  • WshShell.Run "W:\DiskCleanup.reg"
  • WScript.Sleep 100
  • WshShell.SendKeys "{Enter}"
  • WScript.Sleep 100
  • WshShell.SendKeys "{Enter}"
  • FileSystem.CopyFile "W:\*.job", "C:\Windows\Tasks"
  • WshNetwork.RemoveNetworkDrive "W:"

This script is a relatively simple one that basically maps the network share to a drive letter, automates the importation of the Disk Cleanup configuration settings into the local registry, copies the Disk Cleanup and Disk Defragmenter jobs to the local Scheduled Tasks folder, and then disconnects the network drive.

Distributing the tasks
Once you create the script, you can distribute it across the network. Create an e-mail message, address it to all the users to whom you want to distribute the tasks, and attach the script to the message. When the recipients receive the message, all they have to do is double-click the script. Once the script is run, the Windows XP system will be configured to regularly run Disk Cleanup and Disk Defragmenter.

About Greg Shultz

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

Editor's Picks