Windows DevCenter    
 Published on Windows DevCenter (http://www.windowsdevcenter.com/)
 See this if you're having trouble printing code examples


Hands-Free Disk Defragmenting

by Mitch Tulloch
10/04/2005

A fragmented hard drive can quickly become a performance bottleneck for your Windows XP system. The Disk Defragmenter snap-in that's part of the Computer Management console is a "lite" version of Diskeeper's defragmentation software, and unfortunately it doesn't include a built-in scheduling feature. But defrag.exe, the command-line version of the Disk Defragmenter snap-in, can be used together with the Scheduled Tasks wizard (or the schtasks.exe command-line tool) to schedule defragmenting of your hard disk volumes during off hours.

Before you do this, a warning--don't schedule defragmentation at the same time that the Windows XP Backup utility is backing up your drive, or defragmentation won't complete.

Using the Scheduled Tasks Wizard

Using the Scheduled Tasks wizard to schedule a defragmentation is straightforward. Select Start-->All Program-->Accessories-->System Tools-->Scheduled Tasks to open the Scheduled Tasks folder. Double-click on Add Scheduled Task to start the wizard. Then click Next, click the Browse button, and select defrag.exe from the %systemroot%\system32 folder. Then specify a name for your task and a frequency for running it:

Figure 1
Figure 1: Scheduling a defrag operation.

Click Next and finish specifying your defrag schedule:

Figure 2
Figure 2: More scheduling options.

Click Next again and specify the credentials under which your task will run. These credentials should be an administrator account on the local machine:

Figure 3
Figure 3: Specify admin-level credentials for running defrag.exe.

Click Next and select the check box and click Finish to open the advanced properties for the task. Select the Task tab on this properties sheet:

Figure 4
Figure 4: Configuring advanced task properties.

Now you need to edit the command string in the Run box to indicate what drive you want to defrag. For example, to defrag drive C: on your machine, change the command string to look like this:

C:\WINDOWS\system32\defrag.exe c:

You can also force defragmentation to occur even if there is insufficient disk space (Disk Defragmenter normally won't run unless there is at least 15 percent free space on the drive being defragmented). To do this, add the /f (for force) switch to your command string as follows:

C:\WINDOWS\system32\defrag.exe c: /f

You may also want to create a log of your defragmentation task. You can do this by redirecting the output of the command to a text file:

C:\WINDOWS\system32\defrag.exe c: > c:\mylogs\defrag.log

If you want detailed logging you can use verbose mode:

C:\WINDOWS\system32\defrag.exe c: /v > c:\mylogs\defrag.log

Finally, you can select the other tabs on the properties sheet to configure additional advanced settings if they are needed. For example, you could configure the task so it will run only if the computer has been idle for at least 30 minutes, or wake the computer if it is asleep to run the task.

Defragging Multiple Volumes

What if you have several disk volumes you want to defragment? One way of doing this would be to create a separate scheduled task for each volume and stagger these tasks in time so they don't all happen simultaneously. For example, you could defragment your C: drive on Monday, your D: drive on Tuesday, and so on.

Another approach is to create a batch file that contains a series of defrag commands like this:

C:\WINDOWS\system32\defrag.exe c:
C:\WINDOWS\system32\defrag.exe d:
C:\WINDOWS\system32\defrag.exe e:

Then schedule the batch file to run using the Scheduled Tasks wizard and each task will occur in sequence.

Using schtasks.exe

You can also schedule tasks using the schtasks.exe command as follows:

schtasks /create /tn "Defrag Drive C" /tr "C:\WINDOWS\system32\defrag.exe c: /v > c:\mylogs\defrag.log" /sc monthly /mo FIRST /d MON /st 02:00:00 /u MYBOX\Administrator /p Pa$$w0rd /ru SYSTEM

This command will create a task named "Defrag Drive C" which runs defrag in verbose mode, logging output to a text file. The task will run on the first Monday of each month at 2 a.m. and will run defrag.exe using local admin credentials for the computer. The task itself will run under the privileges of the System account.

Before You Defrag

Finally, before you defrag your drive, it's a good idea to make sure you've cleaned it by deleting any unneeded files such as Internet Explorer's temporary files and files moved to your Recycle Bin. You can do this manually using the Disk Cleanup utility, but you can also automate this process so it takes place just before you defragment your drives. To find out how to automate disk cleaning, see Disk Cleanup Hacks.

Mitch Tulloch is the author of Windows 2000 Administration in a Nutshell, Windows Server 2003 in a Nutshell, and Windows Server Hacks.

Windows Server Hacks

Related Reading

Windows Server Hacks
100 Industrial-Strength Tips & Tools
By Mitch Tulloch

Return to the Windows DevCenter.

Copyright © 2009 O'Reilly Media, Inc.