sandy1028 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How to count the number of .txt files in a directory and also subdirecoty recursively. The count should be total number of files of .txt files

Replies are listed 'Best First'.
Re: Recursive count
by velusamy (Monk) on Mar 02, 2009 at 05:16 UTC
    Hi,

    You can do this by using File::Find Module.

Re: Recursive count
by leslie (Pilgrim) on Mar 02, 2009 at 06:53 UTC
    Hi sandy,

    You can use this code for finding the count of .txt files.

    use strict; use warnings; use File::Find; my $dir="/home/Perl/monks"; my $total=0; sub test { ++$total if($File::Find::name =~ /\.txt$/); } find(\&test,$dir); print "Total:$total\n";
Re: Recursive count
by Anonymous Monk on Mar 02, 2009 at 05:17 UTC
      No need to actually loop.
      use File::Find::Rule qw( ); my $txt_count = () = File::Find::Rule ->file() ->name( '*.txt' ) ->in( $dir );