It appears that you're running an Ad-Blocker. This site is monetized by Advertising and by ">User Donations; we ask that if you find this site helpful that you whitelist us in your Ad-Blocker, or make a ">Donation to help aid in operating costs.
Previous Thread
Next Thread
Print Thread
Rate This Thread
#6372 - 07/17/06 09:12 AM Windows Tip: Defrag Made Easy(...Easier) with Perl  
Joined: Mar 2002
Posts: 30
quicksilver Offline
Junior Member
quicksilver  Offline
Junior Member

Joined: Mar 2002
Posts: 30
Everyone who reads this forum either knows or should know that they need to degragment their hard drive regularly if they are using Windows.

For some of us, however, we have to defrag so infrequently that we often forget to do so at all.

One solution is to automate the task by creating a "Scheduled Task"[1] that runs at weekly or monthly intervals depending on your computer usage. This solution bores me.

The best solution would be to defrag only when the disk actually needs defragmenting. Perl to the rescue![3]

If you are running Windows XP, the Disk Defragmenter can be used to simply analyze the disk.[2] By using a small perl script to parse the analysis, we can create a Scheduled Task that will only Defragment our disk when a defined level of File Fragmentation is reached.[4]

The following perl script runs the Disk Defragmenter using the analysis only and verbose arguments (-a and -v, respectively). It then stores the reported percentage of file fragmentation in a variable and compares it to $frag_max. If the actual fragmentation is equal to or greater than $frag_max, the script then runs the Disk Defragmenter utility.

use strict;
my @analysis = `defrag C: -a -v`;
if ($analysis[13] =~ /File fragmentation[^\w]*(\d\d*)/){
  my $frag = $1;
  my $frag_max = 10;
  system "defrag C:" if ($frag >= $frag_max);
NOTE: As many of you can probably see, this script relies on the standard output of the Disk Defragmenter utility. If this output were to change or is different for some reason, this script could break.

ALSO NOTE: You will need to change C: to whatever letter is assigned to the drive you want defragmented. And $frag_max can be changed to whatever percentage of file fragmenation you believe is appropriate.

You can store this script as in the directory of your choosing and create a Scheduled Task that will run it on a monthly, weekly, or even daily basis.

[1] Creating a Scheduled Task is easy as pie. In Windows XP Professional, the Scheduled Task utility is found in Start->Programs->Accessories->System Tools.
There is a Add Scheduled Task wizard that will help you create the Task very easily. (You will most likely need to browse for when prompted to select a program to run.)

[2] I only have Windows XP Professional available to me at the moment. If this does not work on Windows XP Home I will be both surprised and sorry.

[3] Perl is needed for this solution; however, similar scripts can be written in almost any language. If you do not have Perl installed on your system, I suggest downloading ActivePerl .
It is easy to install and fun to use.

[4] I have chosen to use the File fragmentation data rather than Total fragmentation or any of the other metrics that the analysis gives because I believe that File fragmentation is the most important factor in terms of fragmentation that affects performance.

Sponsored Links
#6373 - 07/18/06 09:40 PM Re: Windows Tip: Defrag Made Easy(...Easier) with Perl  
Joined: Feb 2002
Posts: 7,202
Gremelin Offline
Community Owner
Gremelin  Offline

Community Owner

Joined: Feb 2002
Posts: 7,202
Likes: 11
Portland, OR; USA
I use DiskKeeper Pro Premium (WinXP and 2k use the "freeware" version which comes bundled with windows).

It is schedualed to run every wednesday at 6am and to run on a continuous basis until noon. this is set on all machines.

Donate to UGN Security here.
UGN Security, Back of the Web, and VNC Web Services Owner

Member Spotlight
Portland, OR; USA
Posts: 7,202
Joined: February 2002
Show All Member Profiles 
Forum Statistics
Average Daily Posts0
Most Online1,567
Apr 25th, 2010
Latest Postings
by Cyrez on 12/16/17 09:19 AM
Okay WTF?
by HenryMiring on 09/27/17 08:45 AM
The History Thread...
by Gremelin on 08/11/17 07:11 PM
my old account still exists!
by Gremelin on 08/11/17 07:02 PM
My friend NEEDS your HELP!
by Lena01 on 07/21/17 07:06 AM
I'm having fun with this guy.
by gabithompson730 on 07/20/17 08:50 AM
I want to upgrade my phone
by gabithompson730 on 07/20/17 08:49 AM
Top Posters(All Time)
UGN Security 41,392
Gremelin 7,202
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Liked Users (All Time)
§intå× Likes: 3
Black Beard Likes: 1
Cold Sunn Likes: 1
Crime Likes: 1
Cyrez Likes: 1
fleshwound Likes: 1
Ghost Likes: 2
Gremelin Likes: 12
Ice Likes: 1
ninjaneo Likes: 1
Powered by UBB.threads™ PHP Forum Software 7.6.0
(Snapshot build 20170206)