Defrag Hard Drives only when they are fragmented

Martin Brinkmann
Nov 26, 2007
Updated • Nov 8, 2017
Windows, Windows tips
|
4

It does not make sense to start a hard drive defragmentation if the drive is not really fragmented. If a defragmentation is scheduled, it will be executed regardless of state of the drive.

If a drive has no fragmentation whatsoever, or only slight fragmentation that does not impact performance by a lot, then it does not really make sense to run a defrag on it. The defrag tool that ships with Windows can be used to check the fragmentation level of a hard drive, and we are going to use this functionality to create a script that runs defrag only when the hard drive is fragmented.

You can check the fragmentation level of a hard drive using the command line and the command defrag.exe c: /a. The tool then checks the specified hard drive and reports back its fragmentation level.

That's useful to determine a drive's fragmentation status, but won't help you right away as you need to combine the information somehow with the running of the tool so that defrag.exe is only run when necessary.

A script by Ramesh Srinivasan uses the report to determine if a hard drive should be defragmented. The Visual Basic script can be added to the Windows scheduler or simply executed during system startup, shutdown, or by double-clicking the file at any time while the system is running.

I copied the file to a text file and zipped it already. You can download the script here or visit the website above where the script is displayed in text form (Update: article no longer available).

The script enumerates all fixed drives connected to the system. To add the script to the Windows Task Scheduler, download it or create it from scratch if you want, and do the following afterwards.

  1. Tap on the Windows-key, type Task Scheduler and select the entry that comes up. This should open the Windows Task Scheduler on your system.
  2. Select Create Task from the actions listing.
  3. Add a name to it.
  4. Switch to triggers, click on new, and define when you want the script to run. Examples are to run it daily at a specific time, weekly, while the PC is idle, or during start or shutdown.
  5. Switch to actions, select new, and click on browse to add the script you want Windows to run for you.
  6. Click ok afterwards and you are done.

When it is time to execute the script, all drives specified in it will be checked for their fragmentation status. If the drives are fragmented, a defrag operation is run to resolve this.

Summary
Defrag Hard Drives only when they are fragmented
Article Name
Defrag Hard Drives only when they are fragmented
Description
Run a defragmentation software only when a hard drive is fragmented using a simple script and tools that ship with the Windows operating system.
Author
Publisher
Ghacks Technology News
Logo
Advertisement

Tutorials & Tips


Previous Post: «
Next Post: «

Comments

  1. Donn Edwards said on November 27, 2007 at 12:41 am
    Reply

    The Windows defrag utility does more than just remove fragments. It also tries to optimise the position of some startup files.

    I am currently benchmarking all the commercial and freeware defrag products at “The Great Defrag Shootout”. You can also find reviews of most defraggers there as well.

    Regular defrag passes (once a week or once a month depending on the load on your system) can keep your system running smoother. Ideally this should be done when you are not using your computer (i.e. during meetings of after hours) because having to wait for the defrag to finish is a waste of time.

  2. Optimus said on November 26, 2007 at 7:14 pm
    Reply

    With an automatic defragger, you don’t waste any of your time with unnecessaey defrags, so it’s an advantage over scheduling a defrag. An auto defragmenter can monitor fragmentation levels in the background and defrag ONLY if necessary, and automatically, when the system idles, so it’s a much more efficient process.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.