Automated Hard Drive Defragmentation

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.

Read more

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

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven't any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don't stop there - read the Story of Your Life novella it was based on

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments