Batch Files - Defrag

  • UserId: 1
  • Title: Batch Files - Defrag
  • Url: defrag
  • Summary: Batch Files - Defrag
  • Search:
  • DateCreated:
  • DateModified:
  • Published: 1
  • Pageable: 1

Introduction: Why create a batch file for defragmenting, when you can simply run the Windows Defrag program from the Start Menu???

Well, for those who didn't know, it's not possible to schedule defragmentation within Windows 2000, whereas batch files can be scheduled. It also enables you to set the defragger defragging all of your hard-drives and partitions, whilst you go away and do something else - whereas you'd have to manually select each partition prior to defragging using the normal method.

Methods: I've split this article into two sections, the simple method and the advanced method. The simple method simply gets the defrag program started on one, or several drives. The advanced method looks at output to the screen too.

Simple: Create a .txt file in notepad, then type in the following line of text:

Defrag.exe c: -f

Save it as Defrag.bat and execute it (double click on the icon). If you've done it correctly, then a command prompt window should appear, and the Windows defrag program (defrag.exe) will start.

What does the line mean? Defrag.exe calls the defrag executable (or program) c: instructs the defrag program to defrag the c drive -f allows the program to run even if there's not enough free space

I've uploaded an example in .txt format here, it uses the above command several times, once for each of the partitions I need to defrag. Alter the file to suit your system, and then save it as a .bat file.

Advanced: Using the simple method, we can construct a more advanced batch file, that not only runs the defrag.exe program, but also outputs the information to a log file in .txt format. This is especially useful for administrators wishing to keep a log of their tasks for reference.

To edit the simple Defrag.bat file you created, right click on it and choose edit. This will open the batch file in notepad ready for editing.

Next, you can insert the following command on the first line:

@echo off

then on the second line put:

echo message

replacing message with whatever text you wish to put. This will output text to the command prompt, informing the user what is happening. Now you can put the original command:

Defrag.exe c: -f

on the third line of the batch file. For the fourth line, I usually put something like:

echo Defragmentation of Drive C complete.

This appears once the defragging is complete. Now comes the interesting part - output. To output to a .txt file, you need to add the following at the end of each line you wish to be output:

>> "M:\Logs\Defrag\log.txt"

The text between the two " " dictates where the log file will be saved. In the above example, the log file will be named log.txt and it will be saved within a Defrag folder in the Logs folder stored on the M: drive.

Put it all together, save it as a .bat file and execute it again. Once defragmentation has finished, you can read the log file to see if it worked successfully.

I've uploaded an example of the advanced defrag batch file here it defrags each drive in the same way as the simple defrag file does, but it also outputs the information to a log file. Conclusion: There you go, two examples of how to use a batch file to defrag your computer. I'd recommend using the simple method as a means of defragging your computer if you have multiple partitions or drives. There are a few programs out there that claim to automatically defragment all your partitions/drives using the Microsoft engine, but I've found that they don't support Server 2003.

If you opt for the more advanced method, then remember to check all your lines carefully, along with the log file to ensure that it worked successfully.

The output methods you've learnt about in this article can be applied to all batch files too!

Categories that this Post has been filed under

Tags that this Post has been tagged with

Comments

[...] Then move onto one of the following articles: Defrag Drive Mapping Backup Date These articles and resources have been tested working on Windows 2000 SP4, 2003 SP1 and XP Pro SP2. Please use them as a reference only - I shall not be held responsible for any undesired results from using the knowledge within! Feel free to contact me if you have any further questions or comments, or even if you spot a mistake. [...]


[...] move onto one of the following articles: Defrag Drive Mapping Backup Date These articles and resources have been tested working on Windows 2000 [...]


Add a Comment