Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How do I recursively count file sizes in a script that works under both Windows and Linux?

by FiReWaLL (Scribe)
on Jan 21, 2000 at 23:20 UTC ( [id://2311]=perlquestion: print w/replies, xml ) Need Help??

FiReWaLL has asked for the wisdom of the Perl Monks concerning the following question: (directories)

$directory = '/thedir/thatiwantto/count'; print "\n\n"; print CountSize($directory); print "\n\n"; sub CountSize { my ($directory) = $_[0]; my ($ask, $filesize, $item, $size, @raiz); @raiz = glob("$directory/*.*") || warn(); foreach $item (@raiz) { $ask = -d $item; if ($ask) { $size += &CountSize($item); } else { $size += -s $item; } } return($size); }
It should return the total size of all the tree. In Windows, it works, but in Linux, it doesn't. It returns only the current directory file sizes. What should I do?

Originally posted as a Categorized Question.

  • Comment on How do I recursively count file sizes in a script that works under both Windows and Linux?
  • Download Code

Replies are listed 'Best First'.
Re: How do I recursively count file sizes in a script that works under both Windows and Linux?
by tachyon (Chancellor) on Mar 19, 2002 at 14:43 UTC

    File::Find is part of the standard distro, reliable and portable.

    use File::Find; my $dir = 'c:/test'; my $size = 0; find( \&size, $dir ); sub size { # we get each filename in $_ my $file_size = -s $_; $size += $file_size; print "Found $_ $file_size Bytes\n"; } print "$size Bytes in '$dir' and all subdirs\n";
Re: How do I recursively count file sizes in a script that works under both Windows and Linux?
by Crulx (Monk) on Jan 22, 2000 at 01:28 UTC
    Here is a cross-platform way of doing it.
    use strict; my $dir = '.'; print &dir_tree_size($dir) . "\n"; exit 0; sub dir_tree_size { my $dir = shift; my ($i,$total); $total = 0; opendir DIR, $dir; my @files = grep !/^\.\.?$/, readdir DIR; for $i (@files) { if(-d $i) { $total += dir_tree_size($dir . "/$i") } else { $total += -s $i} } return $total; }
    Crulx
      This always return 5333 on SuSE Linux 7.2 Kernel 2.4.4 Mark
      This doesnt seem to work recursively, just counts files in the current directory, not the subdirs.
Re: How do I recursively count file sizes in a script that works under both Windows and Linux?
by Anonymous Monk on Mar 19, 2002 at 10:19 UTC
    # 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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://2311]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (None)
    As of 2024-04-25 00:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found