Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Recursion example (Directory sizing on the side)

by Beatnik (Parson)
on Jan 15, 2001 at 05:06 UTC ( #51836=snippet: print w/replies, xml ) Need Help??
Description: Some recursion example I wrote some time again, it happens to return the directory size. There is probably more than one way to do it, this is just my way :)

sub dodir {
my $dir = $_[0];
if ($dir !~ /\/$/) { $dir .= "/"; }
my @List=readdir(DIR); 
foreach $file (@List) {
my $file = $dir.$file;
if (-d $file) { dodir($file); }
else { $totsize += (-s $file); }
print "Reading $ARGV[0]\n";
print $totsize," bytes\n",($totsize/1024)," Kilobytes\n",($totsize/(10
+24*1024))," Megabytes\n";
Replies are listed 'Best First'.
Re: Recursion example (Directory sizing on the side)
by Fastolfe (Vicar) on Jan 15, 2001 at 08:52 UTC
    This is something File::Find can do pretty easily, as well:
    use File::Find; my $total_size = 0; find(sub { $total_size += -s }, @ARGV ? @ARGV : '.'); print "Total size: $total_size\n";
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://51836]
[atcroft]: stevieb: Also, one other odd question... is it possible to set it up so that all the versions installed can use a single directory that they pull module sources into, rather than duplicating files? (Am I making sense, or do I have too high a CBC
[atcroft]: (caffeine-blood content) atm?)
[atcroft]: .oO(Interesting... I just went to http://blogs.perl. org/, did a search for "Mars", and tried to go to the second page of results, and received instead a "Request-URI Too Large" message.... odd.)

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2017-04-29 05:16 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.