Fri, 17 May 2024


How to count the sum in bytes of all the files contained in a folder?

By: Anton, NetArt Media
Mon, 8 March 2021

my($totalBytes) = 0;
while(<STDIN>)
{
my($line) = $_;
chomp($line);
if($line !~ /<DIR>/) #directories don't count
{
#*** only lines with dates at position 28 ****
if ($line =~ /.{12}((\d| |,){14}) \d\d-\d\d-\d\d/)
{
my($bytes) = $1;
$bytes =~ s/,//; #substitute nothing for comma -- delete commas
$totalBytes += $bytes;
}
}
}
print "$totalBytes bytes in directory.\n";


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox