in reply to how to get total numbers of files in a directory?

Hi. Since you're new, don't forget to visit PerlMonks FAQ and Tutorials (guide to documentation).

File::Find::Rule is one easy way to do what you want. glob is another and opendir/readdir is yet another.

I also suggest perlintro (if you're new to perl)

MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
** The third rule of perl club is a statement of fact: pod is sexy.

  • Comment on Re: how to get total numbers of files in a directory?

Replies are listed 'Best First'.
Re: Re: how to get total numbers of files in a directory?
by broquaint (Abbot) on Dec 09, 2003 at 16:09 UTC
    And some examples of usage

    File::Find::Rule

    use File::Find::Rule; my $cnt = find( file => name => "*.txt", maxdepth => 1, in => "/home/a" );
    glob - Update: fixed bug as noted by sauoq below (it was originally going to be @{ [ glob "*.txt" ] }, that'll teach me for posting in a hurry :)
    my $cnt = () = glob "/home/a/*.txt";
    opendir, readdir + grep
    opendir( my $d, "/home/a" ) or die "ack: $!"; my $cnt = grep { -f and /\.txt$/ } readdir $d;
    HTH

    _________
    broquaint

      my $cnt = glob "/home/a/*.txt";

      Unfortunately, no. It would be more intuitive that way, but the real behavior of that is to set $cnt to the first filename in the list of exansions.

      $ touch {a,b,c}.txt; perl -le 'my $c = glob "*.txt"; print $c' a.txt
      The clunky fix is to force list context...
      $ touch {a,b,c}.txt; perl -le 'my $c =()= glob "*.txt"; print $c' 3

      -sauoq
      "My two cents aren't worth a dime.";