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.
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.
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.