in reply to Perl Directory file extension stats.

Hi sidsinha,

..Please guide me..

Have you tried any? Please let's see it! Then one can guide. Please see How do I post a question effectively?

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
  • Comment on Re: Perl Directory file extension stats.

Replies are listed 'Best First'.
Re^2: Perl Directory file extension stats.
by sidsinha (Acolyte) on Apr 16, 2014 at 08:19 UTC

    After a few trials, I was able to do what I want using the file::find module and a few regex. The below code gives me the number files fore each unique extentions in a directory.

    Now I am trying to find the cumulative sizes of each type of file in the directory. For example if there are 50 Html files, I am trying to find the sum of the size of all 50 HTML files and likewise for every extention that my array @exts contains.

    I tried using file::find::rule->..->name( foreach @ext ) to parse through and find the size, but couldnt get it right. Could someone pls guide?thanks

    use strict; use warnings; use feature "switch"; use File::Basename; use File::Find; use Data::Dumper; $Data::Dumper::Sortkeys=1; my $start_dir = "F:/"; my @exts; find (\&print_all_directories, "$start_dir"); print "Parsing\n"; sub print_all_directories { return if -d; my $full_dir_path = $File::Find::name; my ($ext) = $full_dir_path =~ /(\.[^.]+)$/; my ($name,$path,$suffix) = fileparse($full_dir_path,qr"\..[^.]*$") +; push (@exts, $suffix); } my %counts; $counts{$_}++ for @exts; #print Dumper(\%counts); foreach my $name (sort {$counts{$a} <=> $counts{$b}} keys %counts) { printf "%s %s\n", $name, $counts{$name}; }

      You mean something like this, using hash to get all the statistics you need:

      use warnings; use strict; use File::Find qw(find); use Data::Dumper; # don't fix the directory my $start_dir = $ARGV[0] || '.'; my $ext = qr[\.[^.]+]; find( \&finder => $start_dir ); my %file; # hash to collect statistics sub finder { return if $_ eq '.'; if (/($ext)/) { my $size = -s; $file{$1}++; $file{size}{$1} += $size; } } { local $Data::Dumper::Sortkeys = 1; print Dumper \%file; }

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me
        Excellent! Exactly what I wanted. THanks a lot, been learning a lot from this forum!