gpt4 book ai didi

Perl File::Find::Rule 从数组中排除目录

转载 作者:行者123 更新时间:2023-12-04 23:51:19 42 4
gpt4 key购买 nike

我在数组中有一组目录作为

chdir /tmp;
my @dir=("test" "abc" "def")

我正在寻找一种使用 File::Find::Rule 查找/tmp 中的所有文件的方法递归 ,但不包括@dir 中的文件。

我可以通过以下方式从@dir 获取所有文件
my $rule =  File::Find::Rule->new;
$rule->file;
my @files = $rule->in( @dir );

但我不能否定条件,即排除@dir。我在尝试
chdir /tmp;
@files = File::Find::Rule->new
->file()
->name(@dir)
->prune
->in( "." );

但没有输出。

最佳答案

documentation列出这个例子:

ignore CVS directories

my $rule = File::Find::Rule->new; $rule->or($rule->new
->directory
->name('CVS')
->prune
->discard,
$rule->new);

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



你可能会做同样的事情:
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');

考虑这个例子:
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 从数组中排除目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21431191/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com