Don’t Nag—Just Do It
Users not cleaning up and defragmenting their hard disks? Do it for them the easy way.
May 26, 2004
With the disk capacities available in today's desktops, you probably have few if any hassles with clients that have run low on disk space. Without such hassles, it's easy to forget about disk cleanup and other general maintenance tasks necessary to keep Windows XP and Windows 2000 Professional clients running smoothly and efficiently.
Disk fragmentation and superfluous temporary files can have a detrimental impact on users' systems. XP and Win2K Pro include the Disk Cleanup and Disk Defragmenter tools, but getting users to run those tools is typically an exercise in futility. And if your IT staff is running as thin as most are these days, you probably won't get many volunteers to run these tools for users. In XP, you can automate both Disk Cleanup and Disk Defragmenter and use the built-in task scheduler (i.e., Scheduled Tasks) to run cleanup and defragment operations regularly. In Win2K Pro, you can automate Disk Cleanup but not Win2K Pro's Microsoft Management Console (MMC)-based Disk Defragmenter.
Because deleting unused files before defragmenting a disk is common sense and the best practice, I show you how to automate the Disk Cleanup tool first. Before I begin, though, I should mention that the potential for data destruction exists when you perform a defragmentation. If you don't feel secure in automating the defragmentation process for your clients, you're better off performing the task manually for them.
Disk Cleanup
To clean up unnecessary files in XP and Win2K Pro, you can use Disk Cleanup. You've probably used this tool through its GUI, but you can also run the tool from the command line. You can use command-line parameters (aka switches) to tell Disk Cleanup to delete certain types of files. At first, the command-line process might seem overly complex, but after you get past the learning curve, it makes sense.
When you run Disk Cleanup from the command line, you use the Cleanmgr command, which has three parameters: /d, /sageset, and /sagerun. Using Cleanmgr to automate file cleanup is a two-step process. First, you run the command with the /d and /sageset parameters to specify a set of files to delete, then you run the command again with the /sagerun parameter to perform the actual cleanup.
The /d parameter lets you select the drive on which you want to delete unnecessary files. The /sageset parameter lets you select the types of files you want to delete (e.g., temporary setup files, old Chkdsk files) and associate that set of file types with a unique number. You use the /d parameter with the /sageset parameter, following the syntax
cleanmgr /d X: /sageset:n
where X is the letter of the target drive and n is a unique numeric value from 0 to 65535. Disk Cleanup stores the set of files and its associated number in the registry.
You use the /sagerun parameter to run Disk Cleanup against the set of files you specified with the /sageset parameter. You use the syntax
cleanmgr /sagerun:n
where n is the number representing the set of files you want to delete. The /d parameter doesn't apply when you use the /sagerun parameter.
Let's try setting a group of files to delete, then run Disk Cleanup against those files. Open a command-shell window. Type
cleanmgr /d C: /sageset:100
and press Enter. Figure 1 shows the Disk Cleanup Settings dialog box that appears. In this dialog box, select the types of files you want to delete. When you click OK, Disk Cleanup will save this group of files as set number 100 in the registry. Now type
cleanmgr /sagerun:100
to automatically run the Disk Cleanup tool against those files. As you can see, this process is pretty simple.
File and Frequency Options
Depending on your user environment, you might want to perform light-duty cleanups on a frequent schedule or schedule more intense cleanups at less frequent intervals. As you consider the types of files you can specify for cleanup, you'll probably identify a few general classifications. My personal classification terms for the file-deletion groups are temporary files, operational files, and discretionary user files.
Temporary files. Temporary setup files, temporary Internet files, Windows temporary files, and temporary offline files all fall into the temporary files group. You're safe deleting the files in this group without hesitation.
Operational files. Old Chkdsk files and catalog files for the content indexer fall into the operational files group. These files are also safe to delete.
Discretionary user files. Downloaded program files, files in the Recycle Bin, and offline files fall into the discretionary user files group. Depending on your environment, these files might be off limits for an automated deletion scheme. Downloaded program files, which are in the %systemroot%Downloaded Program Files folder, are typically ActiveX controls and Java applets that the browser downloads when you browse a site that uses them. No real harm occurs when you delete these items because the browser can download them again. However, you must consider frequency of use and the size of the controls or applets before deleting them.
Different users have varying approaches to using the Recycle Bin. Some users are barely aware of its existence, whereas other users access it daily as if it were a typical file-system folder. Because of the latter group of users, you need to be aware of your users' Recycle Bin habits before you automate the deletion of its contents.
The offline files store contains copies of network files that have been made available offline either manually by the user or through a Group Policy setting. Unless you have a problem with users creating excessive offline resources without merit, you should leave these files alone.
After you understand the ramifications of the available file selections, you need to decide how frequently you want to carry out certain deletion operations. For example, like many organizations, you might find that deleting temporary files once a week is adequate but deleting discretionary files once a week is overkill. If you need to perform certain deletions more frequently than others, you have to create multiple selection sets, providing a unique number to each with the /sageset parameter. Keep track of the numbers and their associated settings because no simple way exists to recall that information.
Registry Parameters for /Sageset and /Sagerun
The file-selection settings you configure reside in the client's HKEY_LOCAL_MACHINESOFTWAREMicrosoft
WindowsCurrentVersionExplorerVolumeCaches registry key. Under that key are subkeys that correspond to the available selections in Disk Cleanup. Within those subkeys are StateFlags entries that correspond to the number you provided to the /sageset parameter. For example, Figure 2 shows the Internet Cache Files subkey. In this subkey, note the StateFlags0100 entry, which I configured by following the instructions I previously gave you, and the StateFlags2112 entry, which I set in another /sageset operation. The DWORD value of 2 tells Disk Cleanup to delete the Internet cache files when I run the Cleanmgr command with the /sagerun:2112 parameter. A StateFlags entry with a DWORD value of 0 tells Disk Cleanup to not delete the files associated with the current registry subkey.
The significance of providing these details is that you can configure registry settings on your clients through one of many methods so that the clients have a consistent array of StateFlags values. You can then use a logon script or scheduled task to have all your clients perform the same cleanup operations.
Before I show you how to use Disk Defragmenter, I want to briefly mention one other Disk Cleanup option: file compression. To save disk space, you can use Disk Cleanup to compress old files. However, given today's disk capacities, you might not find many arguments for compressing files. So, whether to compress or not compress is a policy decision that your organization has to make.
Disk Defragmenter
Unfortunately for Win2K Pro users, one limitation of the built-in Disk Defragmenter tool is that you can't deploy it regularly through a script or scheduled task. Fortunately, Microsoft addressed this limitation in XP by adding the Defrag.exe command. The command's basic syntax is
defrag.exe volume
where volume is the disk volume or mount point you want to defragment. Besides the mandatory volume argument, the defrag.exe command has three optional parameters: /a (analyze only), /f (force defragmentation), and /v (verbose output).
To automate Disk Defragmenter, you must use the /f parameter. This parameter tells Disk Defragmenter to proceed with its operations, even if disk space is below 15 percent or if current fragmentation levels don't warrant a defragmentation. Specifically, you can use the command
defrag.exe C: /f
in a batch file or scheduled task. Because of the resource-intensive nature of defragmentation, you should schedule the batch file or task to run during off-hours. Don't run Disk Defragmenter through a logon script.
To keep an eye on how the defragmentation is running, you can write a script that takes advantage of the ERRORLEVEL values that the defrag.exe command returns. ERRORLEVEL values can notify you of low disk space or other problematic conditions that might prevent Disk Defragmenter from completing its mission. They can also notify you of successful defragmentation operations. The Microsoft article "How to Provide Event Logging for the Disk Defragmenter Utility with Windows Script Host" (http://support.microsoft.com/?kbid=294743) tells you how to use Windows Script Host (WSH) to extrapolate the ERRORLEVEL values and create entries in the Windows event log.
If your clients are running Win2K Pro but you still want to automate defragmentation, you might consider using a third-party utility. Even if your clients are running XP, you might consider a third-party utility if you want additional features and better scheduling capabilities. Available defragmentation utilities include Executive Software's Diskeeper 8.0, NORTHERN Parklife's O&O Defrag 6.0, Raxco Software's PerfectDisk 6.0, and Winternals Software's Defrag Manager 2.5.
Proactive Maintenance for Performance and Stability
Although disk-space problems are less of a concern than they used to be, other benefits result from keeping your users' disks clean and defragmented. An automated approach to regularly removing unneeded files and organizing the remaining files is akin to the old adage, "An apple a day keeps the doctor away." And, because you are the proverbial doctor, you'll have time to concentrate on more important tasks.
About the Author
You May Also Like