Perl File :: Find :: Rule从数组中排除目录 [英] Perl File::Find::Rule excluding directories from an array
问题描述
我在数组中有一组目录,
I have a set of directories in an array as
chdir /tmp;
my @dir=("test" "abc" "def")
我正在寻找一种使用File :: Find :: Rule来递归地查找/tmp中的所有文件的方法,但要从@dir中的文件中排除文件.
I am looking for a way using File::Find::Rule to find all files in /tmp recursively, but excluding files from the ones in @dir.
我可以通过
my $rule = File::Find::Rule->new;
$rule->file;
my @files = $rule->in( @dir );
但是我不能否定条件,即排除@dir.我正在尝试
But I cannot negate the condition, that is to exclude @dir. I was trying
chdir /tmp;
@files = File::Find::Rule->new
->file()
->name(@dir)
->prune
->in( "." );
但没有输出.
推荐答案
文档列出了以下示例:
忽略CVS目录
ignore CVS directories
my $rule = File::Find::Rule->new; $rule->or($rule->new
->directory
->name('CVS')
->prune
->discard,
$rule->new);
请注意此处使用空规则.空规则匹配它们看到的任何内容,因此效果是匹配(并丢弃)名为"CVS"的目录或匹配任何内容
Note here the use of a null rule. Null rules match anything they see, so the effect is to match (and discard) directories called 'CVS' or to match anything
您可以probalby做同样的事情:
You could probalby do the same thing:
my @exclude_dirs = qw(test abc def);
my $rule = File::Find::Rule->new;
$rule->or($rule->new
->directory
->name(@exclude_dirs)
->prune
->discard,
$rule->new);
my @files = $rule->in('/tmp');
请考虑以下示例:
Consider this example:
foo@bar:~/temp/filefind> tree
.
├── bar
│ ├── bar.txt
│ └── foobar.txt
├── baz.txt
└── foo
└── foo.txt
2 directories, 4 files
代码如下:
#!/usr/bin/perl
use strictures;
use File::Find::Rule;
use Data::Dump;
my @exclude_dirs = qw(foo);
my $rule = File::Find::Rule->new;
$rule->or($rule->new
->directory
->name(@exclude_dirs)
->prune
->discard,
$rule->new);
my @files = $rule->in('filefind');
dd \@files;
现在我运行这个:
foo@bar:~/temp> perl file-find.pl
[
"filefind",
"filefind/baz.txt",
"filefind/bar",
"filefind/bar/bar.txt",
"filefind/bar/foobar.txt",
]
这篇关于Perl File :: Find :: Rule从数组中排除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!