count files + dirs

From: Simon (simalt_at_totalise.co.uk)
Date: 03/31/04


Date: Wed, 31 Mar 2004 15:40:58 +0100

Hi,

Is there a way to count files and directories as separate entities.

If I use the following program it correctly displays the files in a
directory but also it treats subdirectories as files.

Could I not somehow code it to know that say x are files and n are
subdirectories so I know what is what?

Thanks for you help, newbie in perl.

Below is the test script.
#!/perl/bin/perl -w

use strict;
my ($count);

my $dir = 'd:/active/';

      opendir DIR, $dir or die "Could not opendir $dir; Reason: $!";

my @files = grep !/^\.\.?$/ => readdir DIR;

$count=@files + 1;

      closedir DIR;

   foreach (@files)

          {

               print "File: $_ \n";

           }

print "$count"-1;

result
========
D:\utils\Perl>perl read_dir.pl
File: open relay.txt
File: Server.txt
File: test
3
------------------

In the result "test" is not a file but directory and I am trying to
distinguish this from the files so it would correctly say 2 files and 1
subdirectory.

Much appreciated.

Simon



Relevant Pages

  • Re: count files + dirs
    ... On Wed, 31 Mar 2004, Simon wrote: ... > Is there a way to count files and directories as separate entities. ... > directory but also it treats subdirectories as files. ... > Below is the test script. ...
    (comp.lang.perl.misc)
  • Re: count files + dirs
    ... I know there are a few mistakes in the code but I was just testing it that ... is why the odd code. ... Simon ... >> directory but also it treats subdirectories as files. ...
    (comp.lang.perl.misc)
  • Re: Sorting and moving files to dir for DVD burn
    ... how i can modify this it move the files it organizes into ... use strict; ... use warnings; ...
    (comp.lang.perl.misc)
  • Re: count files + dirs
    ... Simon wrote: ... > Could I not somehow code it to know that say x are files and n are ... > subdirectories so I know what is what? ...
    (comp.lang.perl.misc)