FAQ
Hi Gurus,

I am stuck. I have thousands of jpegs in hundreds of folders and I
would like to do a section by section tally of the number and file
sizes of all the files.

I am using File::Find as it is quick and good at recursively
searching dirs. The files are stored in folder under a letter, then a
section number then the file. So A100/002 is stored in
/usr2/images/a/100/002.jpg. The 'a' will often be a symlink to a
different disk but that hasn't been a problem.

The problem is my section tallies are either getting zero'ed or
accumulating (depending on where I re-initalise the variables). So
instead of getting:

a Size: 6460.38MB Number: 2367
b Size: 8022.31MB Number: 3034
......
I get:
a size: 6460.38MB number:2367
b size: 0MB number:0
e size: 0MB number:0

Heres what I have done:
============== JPEG_COUNT.PL =======
#!/bin/perl

use File::Find;
#use strict;
use diagnostics;

my @dirs = qw(a b e g h m n p r s t v z);
my $images_root = "/usr2/images/";

my @d;
my $total_size = 0;
my $total_num = 0;

# Create an array of all the dirs to check.

foreach my $i (@dirs) {

my $toplevel = "$images_root"."$i"."/";

push(@d,$toplevel);
}

# Cycle through dirs and get listing.

foreach my $dir (@d) {
my $sect_sz = 0;
my $file_size = 0;
my $sect_size = 0;
my $sect_num = 0;

finddepth({wanted => \&find, follow=>1},$dir);

sub find {

if ($_ !~ /jpg/) {
next;
}

++$sect_num;
my ($sz) = ((stat($_))[7] * 0.000001);
$file_size = sprintf("%.2f",$sz);

$sect_sz += $file_size;
$sect_size = sprintf("%.2f",$sect_sz);

# print "DEBUG: \$_=$_, $sect_size\n";


};

my ($let) = substr($dir,-2,1);
print "$let size: ${sect_size}MB number:$sect_num\n";

}

=========================

I could side-step this and simply do `du -k /path/to/jpegs` but I
would like to know what I am doing wrong.

Thanx.
Dp.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 2 | next ›
Discussion Overview
groupbeginners @
categoriesperl
postedMay 14, '04 at 5:35p
activeMay 14, '04 at 8:43p
posts2
users2
websiteperl.org

2 users in discussion

Rob Dixon: 1 post Dermot Paikkos: 1 post

People

Translate

site design / logo © 2022 Grokbase