December 20, 2005
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.
279 GB Total, 116 GB (41%) Free, 12% Fragmented (8% file fragmentation)
279 GB Total, 116 GB (41%) Free, 0% Fragmented (0% file fragmentation)
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 swell foop:
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
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
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.
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".
Microsoft's new Windows OneCare Live will do this for you. It will also backup important files and delete "unnecessary" ones (scary).
"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?
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.
And by PowerToys I mean TweakUI to be specific...
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.
Mac OS X has had it for awhile (I bet you never get tired of hearing that):
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.
"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?