Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

count files in a directory

by mahesnkl (Initiate)
on Oct 10, 2011 at 10:47 UTC ( #930583=perlquestion: print w/ replies, xml ) Need Help??
mahesnkl has asked for the wisdom of the Perl Monks concerning the following question:

$dirname = "/home/oracle/test/Oracle/oradata"; opendir ( DIR, $dirname ) || die "Error in opening dir $dirname\n"; while( ($filename = readdir(DIR))){ print("$filename\n"); $flag=1; } closedir(DIR);
Using this script i had print the file name but need to get the values and mail. Pls help me.

Comment on count files in a directory
Download Code
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 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 j0se (Pilgrim) 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 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 '*';
Re: count files in a directory
by jwkrahn (Monsignor) 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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://930583]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-12-29 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (193 votes), past polls