Alan Haggai Alavi wrote:
On Thursday 28 Oct 2010 06:30:01 Mike Blezien wrote:

I've been out of the programming game for a while and recently got back
into some small programming projects. Just need to figure out if there is
a Perl function to determine the total size of a folder and files? Meaning
once we open/read a directory is to calculate the total, in MB's, the size
of the files in a particular directory folder.

You could make use of File::Find's find function along with stat.

=pod Example

use strict;
use warnings;

use File::Find;

my $size;
my $directory = '/path/to/some/directory';

sub determine_size {
     if ( -f $File::Find::name ) {
         $size += ( stat $File::Find::name )[7];
     }
}


You don't have to stat the same file twice:

sub determine_size {
    if ( -f ) {
        $size += -s _;
    }
}


find( \&determine_size, $directory );

$size /= 1024 * 1024;

print "Total size: $size MB\n";

=cut

Relevant documents to read:
`perldoc File::Find` - http://perldoc.perl.org/File/Find.html
`perldoc -f stat` - http://perldoc.perl.org/functions/stat.html



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to