Use Windows XP’s defrag.exe command-line utility to automate defrags with a batch file:
Step 1: Open Notepad. On the first line of the, type defrag.exe [drive letter]: -f. The script in this example will force a defragment of drive C. If you only wanted to analyse the disk, you could use the –a switch instead.
Step 2: Save the file as defrag.txt to the location of your choice. Once saved, browse to that directory and rename the file to defrag.bat. If you are not prompted with a message about changing the file extension, you will need to make extensions visible in Folder Options.
Step 3: Test the new script by double-clicking on it. A window will appear as shown above. If you don’t want to complete the defragmentation process at this time, press Ctrl +C to cancel the process.
Step 4: To schedule the script, click Start, All Programs, Accessories, System Tools, and click Scheduled Tasks. The Scheduled Tasks window will open. Double-click the Add Scheduled Task icon to launch the wizard.
Step 5: Click Next, and then click the Browse button. Browse to the location of defrag.bat, select it, and click OK. Select the interval at which defrags should occur, for example Monthly, and click Next. Select time and day settings, and click Next.
Step 6: Scheduled Tasks need to run as a user. Your username will probably already be populated, so enter your password and confirm it. Click Next. Click Finish. Then sit back, relax, and wait for your defrag to occur according to the schedule you set out.