in reply to How do I recursively count file sizes in a
script that works under both Windows and Linux?
# crulx's solution has a bug - # readdir needs to have the directory glued back # on once you're looking at files outside the # original directory use strict; my $dir = '.'; print &dir_tree_size($dir) . "\n"; exit 0; sub dir_tree_size { my $dir = shift; my ($i,$total, $f); $total = 0; opendir DIR, $dir; my @files = grep !/^\.\.?$/, readdir DIR; for $i (@files) { $f = "$dir/$i"; if(-d $f) { $total += dir_tree_size($f) } else { $total += -s $f} } return $total; }
|
|---|