Hello,

After a recent discussion about programs that analyze an entire harddrive to graphically display the file distribution on the drive (we couldn't come up with any names), I decided to try one out for myself. The driving force behind me doing so was my curiosity to see what percentage of the drive is used up by MP3, MPEG, and other multimedia files. I wrote the program using File::Find and File::stat, and have questions on both of these.

Below is the code for this program. Many thanks for your wisdom in advance.

use strict; use warnings; use File::Find; use File::stat; use Tk; my @wanted_count; #sizes of desired file types my $other_count; #size of every other file type my $canvas_w = 300; #width of display my $canvas_h = 300; #height of display my $radius = 100; #radius of pie chart my $map = 15; #size of key map image my @color = qw(#FF0000 #00FF00 #0000FF #FF00FF #00FFFF #FFFF00); if ($#ARGV < 1){ print "Usage:$0 <initial dir> <list of extentions>\n"; print "If want to see *.mp3 files, then make extention mp3\n"; print "So far, this program can handle up to 5 extentions\n"; } foreach (@ARGV){ push(@wanted_count, 0); #init each file type } my $mw = MainWindow->new(-title=>"File Monitor", -width=>$canvas_w+180, -height=>$canvas_h+10); my $canvas = $mw->Canvas(-width=>$canvas_w, -height=>$canvas_h, -relief=>"sunken", -borderwidth=>2)->place(-x=>2,-y=>2); finddepth(\&wanted, shift @ARGV); my $total = sum($other_count, @wanted_count); my $last = 0; for (my $i=0; $i<$#wanted_count; $i++){ lookup($last, $i, $ARGV[$i], $wanted_count[$i]); $last += $wanted_count[$i]/$total*360; } lookup($last, $#ARGV+1, "Other", $other_count); MainLoop; sub lookup{ my ($start, $i, $caption, $value) = @_; $canvas->createArc($canvas_w/2-$radius,$canvas_h/2-$radius,$canvas_w +/2+$radius,$canvas_h/2+$radius, -start=>$start, -extent=>$value/$total*359.9, -fill=>$color[$i]); my $img = $mw->Photo('img' . $i, -width=>$map, -height=>$map); $img->put($color[$i], -to=>0,0,$map,$map); $mw->Label(-image=>'img' . $i, -relief=>"flat")->place(-x=>$canvas_w+10, -y=>$i*25); $mw->Label(-font=>"Arial 8", -text=>$caption . value($value))->place(-x=>$canvas_w+10+1.25 +*$map, -y=>$i*25); } sub value { my $out = " ("; my $order = 0; while ($_[0] > 1024){ $_[0] /= 1024; $order++; } $out = sprintf("%s %.3f ",$out,$_[0]); $out .= do { (!--$order) ? "k" : (!--$order) ? "M" : (!--$order) ? "G" : ""; }; return $out . "B)"; } sub sum{ my ($c,@d) = @_; foreach(@d){ $c += $_; } return $c; } sub wanted{ if (-d $File::Find::name){ return; } if (!-f $File::Find::name){ print "$File::Find::name does not exist\n"; return; } for(my $i=0;$i<=$#ARGV;$i++){ if (/\.$ARGV[$i]$/){ $wanted_count[$i] += stat($File::Find::name)->size; return; } } $other_count += stat($File::Find::name)->size; }

Edit by tye to add <readmore>


In reply to Problems using File::stat,Find under windows by gri6507

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.