One small but very welcome improvement in Windows XP Professional over Windows 2000 Professional is the ability to run the disk defragmenter through the command line. Yes, finally you can schedule disk defragging to take place at regular intervals, without resorting to crude VBScript hacks like the one I described in a recent Windows 2000 Professional Here's How column (March, page 106) or having to purchase expensive third-party utilities.
The executable you are looking for is called DEFRAG.EXE, naturally enough, and resides in %windir%\system32. As an aside, there are two other defrag-related executables there as well: DFRGNTFS.EXE and DFRGFAT.EXE. They are carry-overs from Windows 2000, but can't be launched from the command line or via Explorer, so they're no good for what we want to do.
As with all Windows command line utilities, add "-?" to get the help for how to use DEFRAG.EXE:
Windows Disk Defragmenter
Copyright (c) 2001 Microsoft Corp. and Executive Software International, Inc.Usage:defrag.exe
Begin by going to Start-Control Panel-Scheduled Tasks, and use the Add Scheduled Tasks wizard. You won't see DEFRAG.EXE in the list of programs on the second screen of the wizard, so click the Browse button, go into your \Windows\System32 directory, and select it from there.
Then, name the task (e.g., "Weekly Defrag") and pick when to perform it - the frequency (weekly), start time (03:00 a.m.) every 1 week on Saturday, to copy the above example. If you don't run as Administrator for security reasons, Task Scheduler will ask you which user to run the program as. For DEFRAG.EXE, run it as Administrator, and supply the right password.
The Advanced Options dialogue gives you even more options relating to scheduling of tasks - for instance, if you've got a portable computer, you might not want to run DEFRAG.EXE when it's on battery power. Click on the Settings tab and go to the Power Management section on that page; tick the boxes for "Don't start the task if the computer is running on batteries" and "Stop the task if battery mode begins".
You can also tell Task Scheduler to wake the computer from sleeping/hibernation to carry out the disk defragmentation; also, as a safety precaution, stop the task from looping by telling Task Scheduler to kill it if it's been running continuously for more than a certain amount of time.