问题
I would like to count the number of files inside a folder with perl. With the following code I can list them, but i don't know how to count them in perl. Any help?
$dir = "/home/Enric/gfs-0.5.2016061400";
opendir(DIR, "$dir");
@FILES = grep { /gfs./ } readdir(DIR);
foreach $file (@FILES) {
print $file, "\n";
}
closedir(DIR);
回答1:
If you want to just count them, once you have a directory open for reading you can manipulate context on readdir so that it returns the list of all entries, and then assign that to a scalar. This gives you the length of the list, ie. the number of elements
opendir my $dh, $dir;
my $num_entries = () = readdir($dh);
The construct = () =
goes under the name of goatse operator, see it in perlsecret. There are clearer ways, of course, as below.
If you want to count certain kinds of files, pass the file list through grep first, like you do. Since grep
imposes the list context on its input readdir
returns the list of all files, and after filtering grep
itself returns a list. When you assign that to a scalar you get the length of the list (number of elements), ie. your count. For example, for all regular files and /gfs./
files
use warnings;
use strict;
my $dir = '/home/Enric/gfs-0.5.2016061400';
opendir my $dh, $dir or die "Can't open $dir: $!";
my $num_files = grep { -f "$dir/$_" } readdir($dh);
rewinddir($dh); # so that it can read the dir again
my $num_gfs = grep { /gfs./ } readdir($dh);
(This is only an example, with rewinddir
so that it works as it stands. To really get two kinds of files from a directory better read all files into an array and then process that)
Note that readdir
returns the bare filename, without any path. So for most of what is normally done with files we need to prepend it with the path (unless you first chdir
to that directory). This is what is done in the grep
block above so that the -f
file test (-X) has the correct filename.
If you need to use the file list itself, get that into an array and then assign it to a scalar
# Get the file list, then its length
my @files_gfs = map { "$dir/$_" } grep { /gfs./ } readdir($dh);
my $num_gfs = @files_gfs;
Here map builds the full path for each file. If you don't need the path drop map { }
. Note that there is normally no need for the formal use of scalar on the array to get the count, like
my $num_gfs = scalar @files_gfs; # no need for "scalar" here!
Instead, simply assign an array to a scalar instead, it's an idiom (to say the least).
If you are processing files as you read, count as you go
my $cnt_gfs = 0;
while (my $filename = readdir($dh)) {
$cnt_gfs++ if $filename =~ /gfs./;
# Process $dir/$filename as needed
}
Here readdir
is in the scalar context (since its output is assigned to a scalar), and it iterates through the directory entries, returning one at a time.
Note. In all code above I use the example from the question, /gfs./
-- but if that is in fact meant to signify a literal period then it should be replaced by /gfs\./
回答2:
You have the list of files in @FILES
. So your question becomes "how do I get the length of an array?" And that's simple, you simply evaluate the array in scalar context.
my $number_of_files = @FILES;
print $number_of_files;
Or you can eliminate the unnecessary scalar variable by using the scalar()
function.
print scalar @FILES;
回答3:
Try this code for starters (this is on Windows and will include .
, ..
and folders. Those can be filtered out if you want only files):
#!/usr/bin/perl -w
my $dirname = "C:/Perl_Code";
my $filecnt = 0;
opendir (DIR, $dirname) || die "Error while opening dir $dirname: $!\n";
while(my $filename = readdir(DIR)){
print("$filename\n");
$filecnt++;
}
closedir(DIR);
print "Files in $dirname : $filecnt\n";
exit;
回答4:
I know this isn't in Perl, but if you ever need a quick way, just type this into bash command line:
ls -1 | wc -l
ls -1
gives you a list of the files in the directory, and wc -l
gives you the line count. Combined, they'll give you the number of files in your directory.
Alternatively, you can call bash from Perl (although you probably shouldn't), using
system("ls -1 | wc -l");
来源:https://stackoverflow.com/questions/37821571/count-number-of-files-in-a-folder-with-perl