Problem solve Get help with specific problems with your technologies, process and projects.

How to invoke defrag when fragmentation reaches a certain level

A command-line utility in both server and desktop versions of Windows can help determine if a given volume needs to be defragmented. Here's a script to automatically call it up.

The defrag utility in Windows, in both its server and desktop incarnations, has a command-line switch that lets you analyze a given volume and determine if it needs to be defragmented. When you run defrag with the /a option, you'll see a report that looks something like this:

Post Defragmentation Report:
Volume information: Volume size = 283.04 GB

Free space = 196.31 GB
Total fragmented space = 5%
Largest free space size = 125.53 GB
Note: File fragments larger than 64 MB are not included in the fragmentation statistics.
You do not need to defragment this volume.

This last message -- "you do not need to defragment this volume" -- is the important part. Microsoft has gathered a lot of information about how file system fragmentation occurs, and it has added heuristics to the defrag program to detect when it will be effective. If the heuristics determine that a defragmentation is desirable, you'll get a different message:

It is recommended that you defragment this volume.

If you run defrag without the /a option, defrag assumes that you just want to perform a defragmentation run regardless of the circumstances. But this can sometimes cause a major lag, even when you're running it in the background at minimal priority. Running it at off hours only helps a little because your downtime may be prime time for someone halfway around the world who's trying to get work done with that machine.

I recently wrote a short batch script to automatically invoke defrag with the /a switch, process the report results and invoke defrag only if the report indicated it was really required. Since the analysis takes very little time and isn't nearly as disk-intensive as the full defrag pass, it makes sense to invoke that first.

Here's the script:

set reportpath="c:\temp\defragchk.txt"

defrag %1 /a > %reportpath%

find /c "You do not need to defragment this volume" %reportpath%
if %errorlevel%==0 GOTO :end
defrag %1 /u /v

I used the %1 variable to accept a drive letter so that the script could be invoked on any drive in the system. The results from the defrag analysis are written out to a file specified by the %reportpath% variable. I set this to C:\temp\defragchk.txt by default, but if another path works for you, then you can change it.

You can run this script as a replacement for the usual scheduled defrag jobs, but it needs administrator privileges to be effective.

There are a couple minor problems with this method. For example, you need to invoke each drive's defragmentation analysis separately. This may be a problem if you have a lot of lettered drives that change frequently. (It's also possible for this script to fail if you have a drive named "You do not need to defragment this volume," but I don't think most people are going to have that problem.)

My original attempt at this script involved extracting the exact percentage of the fragmentation from the report and then checking a value against that. This could force a defrag at some arbitrary level of fragmentation, but the program's own heuristics about when a full defrag is needed are supposedly pretty trustworthy.

Another version of this script involved using the Win32_DefragAnalysis Class to obtain detailed fragmentation statistics on a given volume. But this approach only works on Windows Server 2003 and higher; it's not available on client versions of Windows at all. The script above is not as elegant as relying on statistics provided by the system's own data providers, but it works much more consistently across platforms, both client and server alike.

Serdar Yegulalp has been writing about personal computing and IT for over 15 years for a variety of publications, including (among others) Windows Magazine, InformationWeek and the TechTarget family of sites.

Dig Deeper on Microsoft Windows 7 operating system

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.