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.

Code:
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 defrag.pl 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 defrage.pl 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,195
Gremelin Offline
Community Owner
Gremelin  Offline

Community Owner

Joined: Feb 2002
Posts: 7,195
Likes: 3
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
Gremelin
Gremelin
Portland, OR; USA
Posts: 7,195
Joined: February 2002
Show All Member Profiles 
Forum Statistics
Forums45
Topics47,470
Posts82,640
Average Daily Posts8
Members2,159
Most Online1,567
Apr 25th, 2010
Latest Postings
Top Posters(All Time)
UGN Security 40,634
Gremelin 7,195
§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: 1
Cold Sunn Likes: 1
Crime Likes: 1
Cyrez Likes: 1
Ghost Likes: 1
Gremelin Likes: 4
Ice Likes: 1
unreal Likes: 1
Top Liked Users (30 Days)
No Data Found
Powered by UBB.threads™ PHP Forum Software 7.6.0
(Snapshot build 20160902)