Automated Hard Drive Defragmentation

December 20, 2005

I tend to ignore defragmenting my hard drive until I belatedly realize it probably looks like swiss cheese by now:

Heavily fragmented hard drive

Wouldn't it be nice if the operating system took care of defragmentation all by itself in the background when I'm not using the computer? Ah, to dream. Until that happens, there's at least one simple workaround.

Windows XP includes a disk defragmenting utility in Start, Programs, Accessories, System Tools. What's less commonly known, however, is that XP also includes the command line version of this utility, defrag.exe:

C:\>defrag c: -f
Windows Disk Defragmenter
Copyright (c) 2001 Microsoft Corp. and Executive Software International, Inc.

Analysis Report
    279 GB Total,  116 GB (41%) Free,  12% Fragmented (8% file fragmentation)

Defragmentation Report
    279 GB Total,  116 GB (41%) Free,  0% Fragmented (0% file fragmentation)

C:\>

It's easy enough to hook up automated disk drive defragmenting using defrag.exe and XP's task scheduler. Just go to Start, Control Panel, Scheduled Tasks and click "Add Scheduled Task". You'll need to edit the task properties manually to set up the proper command line syntax, as pictured here:

defrag scheduled task screenshot

I assume the defrag utility needs to run as an administrator, so set the "Run as" section appropriately.

If you have more than one drive, you can set up multiple scheduled tasks, or use this Windows Script Host file to defragment all the drives in one swell foop:

Option Explicit

Dim sh, fso, d

Set sh = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

For Each d in fso.Drives
    If d.DriveType = 2 Then
        sh.Run "defrag " & d & " -f -v", 1, true
    End If
Next

I'm sure commercial disk defragmenters do a better job, but this one's good enough-- and it's free! However, you may want to complement defrag.exe with SysInternals' PageDefrag utility. Some crucial system files, such as the registry hive, are always locked while the OS is running. But with PageDefrag, you can defragment those files at boot time.

Posted by Jeff Atwood
11 Comments

Yeah, I work with VMs a lot and having their disk image files fragmented can really kill performace, so I've been playing with defrag utils a bit. SysInternals also do a "contig" util that does single-file defrags and that is quite useful for VMs. I'm currently a fan of PerfectDisk after testing DiskKeeper and getting annoyed that it would regularly put the big files in its "too hard" basket (and it's the big files that I care about). However it has better options for playing with the MFT etc. Interested to see whether there are better options.

Moz on December 21, 2005 7:24 AM

I had been using similar VBScript to defrag my HD regularly :) Except that I need to exclude a drive ("S" to be exact for my source codes) being a result of "subst".

William on December 21, 2005 8:24 AM

Microsoft's new Windows OneCare Live will do this for you. It will also backup important files and delete "unnecessary" ones (scary).

a href="http://www.windowsonecare.com/Default.aspx"http://www.windowsonecare.com/Default.aspx/a

Jeff Mastry on December 22, 2005 1:12 AM

"Wouldn't it be nice if the operating system took care of defragmentation all by itself in the background when I'm not using the computer?"

Um... I thought it does? Or what else is that lengthy hard disk rattling I'm hearing occasionally when the HD isn't otherwise busy?

Have you tried leaving your PC alone for a few minutes with a fragmented HD and see what happens?

Chris Nahr on December 22, 2005 2:54 AM

Well, PowerToys for Windows XP has an option called "Optimize hard disk when idle".

"--- allows Windows to rearrange files on the hard disk when the computer is not in use to improve performance."

It seems to be off by default though. At least on my machine.

Fredrik Meyer on December 22, 2005 3:26 AM

And by PowerToys I mean TweakUI to be specific...

Fredrik Meyer on December 22, 2005 3:27 AM

A nice feature of the newer versions of Diskeeper is that they will stop themselves if disk activity becomes high... so if you start a high-demand activity while a background defrag is running, the defrag will back off until the disk demand drops.

mbg on December 22, 2005 8:35 AM

Mac OS X has had it for awhile (I bet you never get tired of hearing that):

a href="http://macslash.org/article.pl?sid=03/10/29/190237"http://macslash.org/article.pl?sid=03/10/29/190237/a

Bill Brown on December 23, 2005 1:01 AM

Windows is just trying to do what linux has been doing right for years (cleaning out all the temp files, etc). Except it's not necessary to defragment in linux.

Roger on September 28, 2006 10:01 AM

I can't believe you wrote that and didn't mention Buzzsaw and DIRMS from http://www.dirms.com/

dhanson865 on December 18, 2007 3:27 AM

"the operating system took care of defragmentation all by itself in the background when I'm not using the computer?"

What if the FS was designed to have minimal fragmentation?

Who ever defrags their ext3, reiserfs, xfs... partitions?

Adam Marchetti on July 12, 2010 1:28 PM

The comments to this entry are closed.