如何在Perl脚本中递归地查找文件/文件夹? [英] How to find files/folders recursively in Perl script?
问题描述
我有一个perl脚本,我已经写了搜索文件存在我的Windows文件夹,递归。我输入搜索文本作为perl脚本运行时参数来查找名称中包含此文本的文件。 perl脚本如下:
使用Cwd;
$ file1 = @ARGV [0];
#@ res1 = glob* test *;
#@ res1 = glob$ file1 *;
@ res1 = map {Cwd :: abs_path($ _)} glob$ file1 *;
foreach(@ res1)
{
print$ _\\\
;
}
但是这并不是递归地搜索所有的子目录。我知道 glob
不会递归匹配。
所以尝试使用模块 File :: Find
和函数 find(\& wanted,@directories);
但是我得到一个错误, code> find() undefined。从我从帮助中读到的内容,我认为在Perl安装中默认定义了 find()
函数,其中有一些基本的代码来查找文件夹/文件。是不是正确?
问题是,在上面的perl脚本中,如何递归搜索文件/文件夹?
第二个问题,我发现 perldoc< module>帮助
没有关于在该模块中使用某个函数的示例,这将使其清楚。
您可以指出一些很好的帮助/文档/书籍,它们使用不同perl模块的各种perl函数,并清楚地说明这些模块函数的用法。另一个优秀的模块是使用 File :: Find :: Rule ,它隐藏了一些 File :: Find ,同时展示相同的丰富功能。
使用File :: Find :: Rule;
使用Cwd;
my $ cwd = getcwd();
my $ filelist;
sub buildFileIndex {
open($ filelist,>,filelist.txt)死!
#文件查找规则
my $ excludeDirs = File :: Find :: Rule->目录
- >名称('demo','test','sample ','3rdParty')#提供具体的目录列表* *不扫描
- > prune#不要进入它
- > discard; #不报告
$ b $ my $ includeFiles = File :: Find :: Rule->文件
- >名称('* .txt','* .csv') ; #按文件扩展名搜索
$ b $ my @files = File :: Find :: Rule->或($ excludeDirs,$ includeFiles)
- > in($ cwd);
print $ filelist map {$ _\\\
} @files;
返回\ $ filelist;
}
I have a perl script which I have written to search files present in my windows folders, recursively. I enter the search text as the perl script runtime argument to find a file having this text in it's name. The perl script is as below:
use Cwd;
$file1 = @ARGV[0];
#@res1 = glob "*test*";
#@res1 = glob "$file1*";
@res1 = map { Cwd::abs_path($_) } glob "$file1*";
foreach (@res1)
{
print "$_\n";
}
But this is not searching all the sub-directories recursively. I know glob
doesn't match recursively.
So tried using module File::Find
and the function find(\&wanted, @directories);
But I got a error saying find()
undefined. From what I read from help, I thought find()
function is defined by default in Perl installation, with some basic code to find folders/files. Isn't it correct?
Questions is, in the above perl script, how do I search for files/folders recursively?
Second questions, I found that perldoc <module> help
does not have examples about using a certain function in that module, which would make it clear.
Can you point to some good help/document/book for using various perl functions from different perl modules with clear examples of usage of those module functions.
Another excellent module to use is File::Find::Rule which hides some of the complexity of File::Find while exposing the same rich functionality.
use File::Find::Rule;
use Cwd;
my $cwd = getcwd();
my $filelist;
sub buildFileIndex {
open ($filelist, ">", "filelist.txt") || die $!;
# File find rule
my $excludeDirs = File::Find::Rule->directory
->name('demo', 'test', 'sample', '3rdParty') # Provide specific list of directories to *not* scan
->prune # don't go into it
->discard; # don't report it
my $includeFiles = File::Find::Rule->file
->name('*.txt', '*.csv'); # search by file extensions
my @files = File::Find::Rule->or( $excludeDirs, $includeFiles )
->in($cwd);
print $filelist map { "$_\n" } @files;
return \$filelist;
}
这篇关于如何在Perl脚本中递归地查找文件/文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!