Step-by-Step: Defragment Multiple Hard Drives Using a Batch File
 December 7, 2003

Note: This guide is for Windows XP only.  Windows XP includes a version of Defrag.exe that has a command line interface, which we will be using for this guide.  The other versions of Windows (95, 98, Me, and 2000) do not have a command line version of Defrag.exe.

Defragmenting your hard drive is something we all know we should do, but probably never do enough.  While mild disk fragmentation is not usually a problem, a computer with a terribly fragmented hard disk will suffer large performance hits.  The reason people don't run defrag (the built-in file defragment program) more often is that it takes a while to run and while the program is running, you can't really use the computer.  Also, if your computer has multiple hard drives, you have to defrag each disk individually which takes even more time.  Given this, people end up not defragmenting at all.

In this step-by-step guide, I will show you two different defragment methods which you can use.

The first method is for computers that get turn on and turn off every day and have multiple hard drives.  This method will allow you to defrag all of the hard drives on your computer at once without any intervention on your part.

The second method is for computers that are on 24 hours a day.  This method extends the previous method by scheduling daily defrags of all the hard drives on your computer.

Let's start!

In these examples, we're going to use the command line version of defrag.exe instead of the graphical version.  With the command line version, we can write batch files which are scripts which tell the computer what to do, one line at a time.

Let's first see what the options are for the command line version of defrag.exe. 

Start --> Run

At the dialog box, type in "cmd" to bring you to a DOS-like command window.



Type "defrag -?" and hit the "Enter" key.


You'll see this help menu:

defrag <volume> [-a] [-f] [-v] [-?]
volume  drive letter or mount point (d: or d:\vol\mountpoint)
  -a     Analyze only
  -f      Force defragmentation even if free space is low
  -v      Verbose output
  -?     Display this help text

Basically, this means that to defrag a drive, we type in "defrag" followed by the drive letter (volume) and whatever additional options.  Of all the attributes, we're only really interested in "-f" which forces defragmentation even if free space is low.  Without this option enabled, the defrag program would stop and ask you to continue or not.

So for example, to defrag drive c: at the command prompt, we would type in:

defrag c: -f

But how do we defrag multiple drives at one time?  This is where a batch file comes into play.  A batch file is simply a list of commands that are performed one after another in the order you input them.

To do this, open up the "Notepad" program.

Start --> Programs --> Accessories --> Notepad


First thing to do is make sure that the "Word Wrap" option is checked under the "Format" menu. 

Type in "defrag c: -f" and for each additional hard drive that you want to defrag, type the same thing, but replacing the "c:" with the drive letter of each of your hard drives.

For example, on my computer, I have 3 hard drives with the letters "c:", "d:", and "e:".

Note that you have to hit the "Enter" key after every line.  My notepad entry would look like this:


Now let's save this thing as a batch file.  In the notepad application, go to File --> Save.

You'll see this:


Type in "defrag-hd.bat".  Notice the "" marks.  You need to include the "" marks in the line.  This forces notepad to save the file with the extension as you type it, not with the normal default of ".txt".  Save the file to where ever you like.  Click "Save".


To run the batch file, just double-click on the icon labeled "defrag-hd.bat" and a DOS command window will open and defrag will start.

With this batch file, you can simply run the batch file and defrag all of your hard disks at once.  No fuss, no muss.

You should run this program every so often just to keep your file system healthy.  The first time you run the defrag, it may take a long time, but if you run defrag regularly, the process of defragmenting your hard drive won't take that long.

If you leave your computer on 24/7 then you can take this guide further by automating daily defrags using Windows XP's Scheduled Tasks.  Here's how: Step-by-Step: Automatic Daily Defragmenting Using Scheduled Tasks

Step by Step Menu

Additional Information