Automated Hard Drive Defragmentation

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

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:

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 fell swoop:

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.

Jeff Atwood

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Let's be kind to each other. Find me https://infosec.exchange/@codinghorror

⏲️ Busy signing you up.

❗ Something's gone wrong. Please try again.

✅ Success! Check your inbox (and your spam folder, just in case).

Recent Posts

map of the United States via rgmii.org showing all 3,143 counties by rural (gold) / metro (grey) and population

Launching The Rural Guaranteed Minimum Income Initiative

It's been a year since I invited Americans to join us in a pledge to Share the American Dream: 1. Support organizations you feel are effectively helping those most in need across America right now. 2. Within the next five years, also contribute public dedications of time or

By Jeff Atwood ·
Comments
Let's Talk About The American Dream

Let's Talk About The American Dream

A few months ago I wrote about what it means to stay gold — to hold on to the best parts of ourselves, our communities, and the American Dream itself. But staying gold isn’t passive. It takes work. It takes action. It takes hard conversations that ask us to confront

By Jeff Atwood ·
Comments
Stay Gold, America

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood ·
Comments
I’m feeling unlucky... 🎲   See All Posts