Your browser does not seem to support CSS. If images appear below, please disregard them.
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,198
Gremelin Offline
Community Owner
Gremelin  Offline

Community Owner

Joined: Feb 2002
Posts: 7,198
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
SC, usa
Posts: 506
Joined: March 2002
Show All Member Profiles 
Forum Statistics
Average Daily Posts3
Most Online1,567
Apr 25th, 2010
Latest Postings
shopping for jewelry with something above
by mikejoey on 04/12/17 08:01 AM
PANDORA North The usa
by mikejoey on 04/12/17 07:38 AM
Pandora’s birthstone products will always
by mikejoey on 04/12/17 07:14 AM
the newest Pandora necklaces crafting facility
by mikejoey on 04/12/17 06:53 AM
an Air Jordan 4 by Off White is in
by mikejoey on 04/12/17 06:35 AM
belonging to the adidas NMD XR1
by mikejoey on 04/12/17 05:50 AM
Other detailsThe adidas Originals Celebrity
by mikejoey on 04/12/17 05:35 AM
The DNA on the NMD model is actually
by mikejoey on 04/12/17 05:18 AM
in which adidas NMD XR1 “Zebra” supplying
by mikejoey on 04/12/17 04:05 AM
by Gremelin on 01/14/17 07:03 PM
Top Posters(All Time)
UGN Security 41,138
Gremelin 7,198
§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
Top Liked Users (30 Days)
No Data Found
Powered by UBB.threads™ PHP Forum Software 7.6.0
(Snapshot build 20170206)