Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: direcotories and spaced used

by nop (Hermit)
on Nov 06, 2000 at 21:45 UTC ( #40195=note: print w/replies, xml ) Need Help??

in reply to direcotories and spaced used

Thanks! I used Albannach's suggestion, though it seems I need to prepend the directory name explicitly. Anyway, the code below works... thanks gang!
use strict; my $dir = shift; if (!$dir) {die "perl $0 [startdir]\n";} my %size; &dir_tree_size($dir); foreach my $dir ( map { $_->[1] } sort { $b->[0] <=> $a->[0] } map { [$size{$_}, $_ ] } keys %size) { print "$size{$dir}\t$dir\n"; } sub dir_tree_size { my $dir = shift; warn "$dir\n"; my ($i,$total); $total = 0; opendir DIR, $dir; my @files = grep !/^\.\.?$/, readdir DIR; for $i (@files) { if(-d $dir . "\\" . $i) { $total += &dir_tree_size($dir . "\\$i") } else { $total += -s $dir . "\\" . $i} } $size{$dir} = sprintf("%8d K", $total / 1024); return $total; }

Replies are listed 'Best First'.
RE: Re: directories and spaced used
by Albannach (Prior) on Nov 06, 2000 at 22:12 UTC
    Seems to work nicely on this NT box. Since I hate warnings, I took the ' K' out of your sprintf() and added to your print line.

    Also, I don't think your return $total; does anything that wouldn't be done without it, does it?

    Update: Ok, count me stupid (I already mentioned Monday, right?)! I guess the non-summing result looked useful enough to me? Thanks for clearing that up for me nop!

    Just for the record, the result will be total of file size, not space used (the Windows Properties box reports both values).

RE: Re: direcotories and spaced used
by nop (Hermit) on Nov 06, 2000 at 23:30 UTC
    Without the  return $total , directories don't inherit the size of their subdirectories... without it, the
    $total += &dir_tree_size($dir . "\\$i")
    does nothing....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://40195]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2018-03-21 03:26 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (263 votes). Check out past polls.