我是Perl的新手,我正在努力学习这门语言,但我很难做一些我觉得很简单的事情。
我已经能够获得一个可以计算目录中文件数量的脚本。我想增强脚本以递归计算任何子目录中的所有文件。我搜索过并找到了一些GLOB和File :: Find的不同选项,但是还没能让它们工作。
我目前的代码:
#!/usr/bin/perl
use strict;
use warnings;
use Path::Class;
# Set variables
my $count = 0; # Set count to start at 0
my $dir = dir('p:'); # p/
# Iterate over the content of p:pepid content db/pepid ed
while (my $file = $dir->next) {
next if $file->is_dir(); # See if it is a directory and skip
print $file->stringify . "\n"; # Print out the file name and path
$count++ # increment count by 1 for every file counted
}
print "Number of files counted " . $count . "\n";
任何人都可以帮我增强此代码以递归搜索任何子目录吗?
答案 0 :(得分:2)
File::Find模块是您递归操作的朋友。这是一个计算文件的简单脚本:
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
use File::Find;
my $dir = getcwd; # Get the current working directory
my $counter = 0;
find(\&wanted, $dir);
print "Found $counter files at and below $dir\n";
sub wanted {
-f && $counter++; # Only count files
}