Re: question about # of files in a directory



On Sep 28, ZHAO, BING said:

Is there a way to call or maybe get the # of files in a directory?

The simplest way is to use opendir() and readdir().

  opendir my($dh), $path or die "can't opendir $path: $!";
  my @files = readdir $dh;
  closedir $dh;

Now you have the files from the directory $path in the array @files. The number of elements is the number of files.

--
Jeff "japhy" Pinyan        %  How can we ever be the sold short or
RPI Acacia Brother #734    %  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %    -- Meister Eckhart
.



Relevant Pages

  • Re: how do I know if its an array?
    ... check boxes. ... How will I know if I have an array or not? ... RPI Acacia Brother #734 % the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? ...
    (perl.beginners)
  • Re: References Question
    ... the second is getting an element in the array referred to by $this. ... print $ref[2]; ... RPI Acacia Brother #734 % the cheated, we who for every service http://japhy.perlmonk.org/ % have long ago been overpaid? ...
    (perl.beginners)
  • RE: Cant use subscript in angle brackets
    ... Second of all, you should not be using `ls ...` for this, because Perl offers you opendir, readdir(), and closedir. ... Now you have all the file *names* in an array. ... of the array both as paths to files AND as the filehandles themselves. ... Now for every element in @dbfiles, the matching element in @fh is a filehandle for it. ...
    (perl.beginners)
  • Re: fileorder of readdir()
    ... Subject: fileorder of readdir() ... > An alternative is to read the files into an array and sort them, ... Try to put all folder content with readdirto an array, ... function cmp ...
    (comp.lang.php)
  • Re: reading directories
    ... "Robin" wrote in message ... > When you readdir will it sort the files of into the array ie: ...
    (comp.lang.perl.misc)