AutoDefrag

As a direct consequence of the combination of my sociable nature and interest in computers, I often find myself being asked general computing questions. Certainly this makes sense because while most everyone I know owns and uses a computer, they aren’t all as fortunate as I am to have it completely consume them 24/7.

One of the questions I am asked most goes something like this:

“My computer’s running really slow, can you fix it?”

I like to respond with:

“Well, sure I can fix it… but let’s see if you can fix it so I don’t have to.” ::smiles::

So here’s the deal: nine times out of ten it turns out the person asking has never even heard of defragmentation. I suppose that’s a good thing in one light… but in a world of mass data installations, content creation, and downloading, followed, of course, by a stroke of the delete key and the emptying of the Recycle Bin, defragmenting your hard drive is a must.

For those of you who don’t know, the memory space on your hard drive is treated like a contiguous row of slots that can be filled with data. When you install a program or create a new file of some sort, your operating system (say, Windows XP) places the data into one or more of those slots.

In the interest of speed, your system will do its best to keep all the data together. This is because your hard drive cannot grab data from two separated slots without spending time seeking from one to the other. The further they are from each other the longer it takes for your hard drive to physically move to and access that data.

There are a couple of reasons to defrag your hard drive.

  1. It cleans fragmented files that may be spread out across your disk.
  2. It compacts the data on your disk.

The second point is important because not only does it mean that you will have potentially shorter seek times to retrieve your data, but also it leaves you with more contiguous free space on your drive.

Let’s look at an example. Forget about megs and gigs and let’s just say that your computer has only 16 slots of space. Let’s also say that your operating system takes up four of those slots and whatever documents you have (including all of those old papers from undergrad) take up three slots.

Now let’s install something… say the game Portal everyone keeps talking about. Let’s say that takes up three slots as well.

Portal was fun, but you miss the old days of gaming and want to install RollerCoaster Tycoon. The box says it requires three slots and since you only have ten of your sixteen slots filled surely you have enough room on your hard drive…

Much to your dismay, you’re met with a message that you don’t have enough free space on your drive. What!? Of course you do… simple math tells you that 10 + 3 =13… which, last time you checked, is less than 16! What gives?!

The issue has to do with memory layout. As you can see in Figure 2, there is no contiguous block of four empty slots to use for RollerCoaster Tycoon.

Okay… but you still want to install RollerCoaster Tycoon because you love watching those kids get sick from your diabolical rides! And, you think it’s stupid that you would to have to uninstall Portal especially since you still haven’t completed all the bonus levels. If only there was a way to compact the data you already have on your hard drive…

Hopefully I’ve helped make the case that if you haven’t ever defragmented your hard drive… you should. So, now let’s get to it…

Manual Defrag

Defragmenting is easy. The only problem is, depending on how long its been since you last ran it and how large your hard drive is, it can take a long time. Also, it’s a good idea not to have other programs running while defrag is running, so you should start it up and leave your computer alone until its done.

Under My Computer:

  1. Right click on your hard drive.
  2. Select Properties.
  3. Choose the Tools tab.
  4. Press the “Defragment Now…” button.

Now you’re faced with a window that looks like the following:

Press the “Defragment” button and let it run. Go for a walk, play with the cat, pass out on the couch… all good options while waiting for your machine to defragment.

AutoDefrag

Okay so if you’re like me, you’re thinking that waiting for this thing sucks… and it does. My time is valuable and if there’s one thing that I’ve learned over the past few years it’s that if I have to do something more than twice I need to figure out how to automate it because I will undoubtedly have to do it again. So let’s automate it…

Automation is one of the computer’s biggest and under-utilized strengths. In this case, we’re going to make the computer defragment our hard drive when we’re using the computer the least… in other words: when we’re sleeping.

To do this, we’re going to setup a Scheduled Task.

Under the Start button, go to Settings > Control Panel.

In the Control Panel, double-click on Scheduled Tasks (you may need to tell it to use the “Classic View” of the Control Panel).

Now double-click on “Add Scheduled Task”.

In the Scheduled Task Wizard:
Click Next.

Use the “Browse” button to find the defrag program (it is not in the list of available programs provided). The location for WinXP is:

C:\WINDOWS\system32\defrag.exe

Once selected, the next window of the wizard will ask you to name the task and choose its schedule. Here I’ve chosen to call it “Nightly Defrag” and set it to perform the task daily.

Next, the wizard asks you to fill in the details about its schedule. Below, I have set it to run at 5AM every day, starting tomorrow morning.

In the next window, you will need to fill in your user data. The username should already be filled in for you. If you don’t have a password on your account I suggest you get one.

In the final window, be sure to check the “Open advanced properties for this task…” checkbox and hit Finish.

In the Advanced Properties window, you will need to add a couple parameters to the Run line:

C:\WINDOWS\system32\defrag.exe C: /f

This says that you want to defragment your C: drive and that you want to force it to run even if the disk fragmentation is less than 15%. Of course, you should change your drive letter to match the drive you wish to defrag accordingly.

When you hit OK, it may ask you to re-enter your password.

And that’s it! You now have an automated defragmentation task! BOOYAH!

Further Steps

Certainly, further steps can be taken on top of this. I like the way Scheduled Tasks has been setup in WinXP, but I don’t like the idea that I need to go looking to find out if the task fails. One idea is to use a scripting language, such as Python, to wrap the defrag task and act accordingly if the operation succeeds or fails… hmm… that sounds pretty awesome! Okay, we’ll tackle this in another entry.

Comments are closed.