UGN Security Forums
My ProfileMember DirectoryLogin
Search our ForumsView our FAQView our Site Rules
View our CalendarView our Active TopicsGo to our Main Page

UGN Security Store
 

Network Sites UGN Security, The GoNix Initiative, Elite Web Gamers, Back of the Web, EveryDay Helper, VNC Web Design & Development
December
Su M Tu W Th F Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Sponsored Links
Latest Postings
Latest Reviews
Topic Options
Rate This Topic
#6372 - 07/17/06 05:12 AM Windows Tip: Defrag Made Easy(...Easier) with Perl
quicksilver Offline
Junior Member

Registered: 03/05/02
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.

Top
Sponsored Links
      
#6373 - 07/18/06 05:40 PM Re: Windows Tip: Defrag Made Easy(...Easier) with Perl
Gremelin Offline

Community Owner
*****

Registered: 02/28/02
Posts: 7193
Loc: 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, Elite Web Gamers & VNC Web Design Owner

Top

Moderator:  Infinite 
Featured Member
Registered: 10/28/14
Posts: 1
Forum Stats
2152 Members
46 Forums
36281 Topics
71451 Posts

Max Online: 1567 @ 04/25/10 05:20 AM
Top Posters
UGN Security 29442
Gremelin 7193
§intå× 3255
SilentRage 1273
Ice 1146
pergesu 1136
Infinite 1041
jonconley 955
Girlie 908
unreal 860
Newest Members
cdefgh368568, HushHush, golqm, Tim050, Gecko666
2151 Registered Users
Who's Online
0 registered (), 318 Guests and 356 Spiders online.
Key: Admin, Global Mod, Mod
Latest News


Donate
  Get Firefox!
Get FireFox!