Perl File::Find::在目录中查找最新文件的规则
我试图在特定路径(Perl File::Find::在目录中查找最新文件的规则,perl,file,find,rule,Perl,File,Find,Rule,我试图在特定路径($output)下获取每个目录(每个项目)中的最新文件列表,但不包括单个目录旧目录 use strict; use warnings; use Data::Dump; use File::Find::Rule; my $output = "/abc/def/ghi"; my @exclude_dirs = qw(OLD); my $rule = File::Find::Rule->new; $rule->or($rule->new -&g
$output
)下获取每个目录(每个项目)中的最新文件列表,但不包括单个目录旧目录
use strict;
use warnings;
use Data::Dump;
use File::Find::Rule;
my $output = "/abc/def/ghi";
my @exclude_dirs = qw(OLD);
my $rule = File::Find::Rule->new; $rule->or($rule->new
->file()
->name(@exclude_dirs)
->prune
->discard,
$rule->new);
my @files = $rule->in("$output");
dd \@files;
我的目录结构:
My Dir Structure:
/abc/def/ghi
├── project1
│ ├── 2013
| ├── file1_project1.txt
│ └── 2014
| ├── foobar__2014_0912_255.txt
| ├── foobar__2014_0916_248.txt
├── project2
│ ├── 2013
| ├── file1_project2.txt
│ └── 2014
| ├── foobarbaz__2014_0912_255.txt
| ├── foobarbaz__2014_0916_248.txt
└── OLD
└── foo.txt
电流输出:/abc/def/ghi/Project1/
/abc/def/ghi/Project1/2013
/abc/def/ghi/Project1/2013/file1_Project1.txt
/abc/def/ghi/Project1/20l4
/abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt
/abc/def/ghi/Project1/2014/foobar_2014_0916_248.txt
/abc/def/ghi/Project2
/abc/def/ghi/Project2/2013
/abc/def/ghi/Project1/2013/file2_Project1.txt
/abc/def/ghi/Project2/2014
/abc/def/ghi/Project2/2014/foobarbaz_2014_0912_255.txt
/abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt
所需输出:
/abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt
/abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt
以下使用将获得文件的完整列表
您可以构建数组散列以保存结果,然后筛选出每个项目的最新文件:
use strict;
use warnings;
use Data::Dump;
use File::Find::Rule;
my $basedir = "testing";
my @exclude_dirs = qw(OLD);
my $rule = File::Find::Rule->new;
$rule->or( $rule->new->directory()->name(@exclude_dirs)->prune->discard, $rule->new )->file;
my @files = $rule->in($basedir);
dd @files;
产出:
(
"testing/project1/2013/file1_project1.txt",
"testing/project1/2014/foobar__2014_0912_255.txt",
"testing/project1/2014/foobar__2014_0916_248.txt",
"testing/project2/2013/file1_project2.txt",
"testing/project2/2014/foobarbaz__2014_0912_255.txt",
"testing/project2/2014/foobarbaz__2014_0916_248.txt",
)
testing/project2 - testing/project2/2014/foobarbaz__2014_0916_248.txt
testing/project1 - testing/project1/2014/foobar__2014_0916_248.txt
要完成筛选,以下附录使用:
产出:
(
"testing/project1/2013/file1_project1.txt",
"testing/project1/2014/foobar__2014_0912_255.txt",
"testing/project1/2014/foobar__2014_0916_248.txt",
"testing/project2/2013/file1_project2.txt",
"testing/project2/2014/foobarbaz__2014_0912_255.txt",
"testing/project2/2014/foobarbaz__2014_0916_248.txt",
)
testing/project2 - testing/project2/2014/foobarbaz__2014_0916_248.txt
testing/project1 - testing/project1/2014/foobar__2014_0916_248.txt