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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: count files in a directory
by jwkrahn (Abbot) on Oct 10, 2011 at 11:59 UTC
    count files in a directory

    This will give you the count of "files" in a directory:

    my $dirname = "/home/oracle/test/Oracle/oradata"; opendir my $DIR, $dirname or die "Error in opening dir '$dirname' beca +use: $!"; my $count = grep -f "$dirname/$_", readdir $DIR; closedir $DIR;
Re: count files in a directory
by pvaldes (Chaplain) on Oct 10, 2011 at 11:01 UTC
    need to get the values and mail

    sorry, I don't understand the question... what mail? this is only a list of files. If you use a list in a scalar context you will get the number of elements

    my $dirname = "/home/oracle/test/Oracle/oradata"; opendir (my $DIR, $dirname ); my $num_of_files = () = readdir $DIR; print "Found ", ($num_of_files - 2), " files in the dir $dirname\n"; closedir $DIR;
    Updated: don't add ".." and "." to the sum
Re: count files in a directory
by reisinge (Hermit) on Oct 10, 2011 at 11:07 UTC
    To count the number of files in mydir you could use:
    #!/usr/bin/perl use strict; use warnings; my $dirname = "/mydir"; opendir ( DIR, $dirname ) || die "Error in opening dir $dirname\n"; my $count; while ( (my $filename = readdir(DIR)) ) { next if $filename =~ /^\.{1,2}$/; # skip . and .. $count++; } closedir(DIR); print "$count files live in '$dirname'\n";

    Update: a sample mail sending code.

    Have a nice day, j

Re: count files in a directory
by keszler (Priest) on Oct 10, 2011 at 11:05 UTC

    need to get the values and mail Read values and email addresses from the files? Count the files and send email with the count? Pls help us help you.

Re: count files in a directory
by manuelsv (Initiate) on Oct 10, 2011 at 11:51 UTC

    Maybe a more concise approach, using globbing ?

    #!/usr/bin/perl use strict; use warnings; my $dir = '/home/oracle/test/Oracle/oradata'; my @files = <$dir>; my $numfiles = scalar @files; print "Files in $dir: $numfiles\n";
    Hope it helps.
      It might work if you add
      chdir $dir or die "Cannot chdir to '$dir': $!"; my @files = glob '*';