[英]Perl File::Find::Rule to find latest file in directories
I am trying to get the list of latest files in each dir(for each project) under a specific path ( $output
) , excluding a single dir OLD
我正在尝试在特定路径(
$output
)下获取每个目录中(每个项目)的最新文件列表,但不包括单个目录OLD
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: 我的目录结构:
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
Current Output: 电流输出:
/abc/def/ghi/Project1/ / ABC / DEF / GHI / PROJECT1 /
/abc/def/ghi/Project1/2013 / ABC / DEF / GHI / PROJECT1 / 2013
/abc/def/ghi/Project1/2013/file1_project1.txt /abc/def/ghi/Project1/2013/file1_project1.txt
/abc/def/ghi/Project1/20l4 / ABC / DEF / GHI / PROJECT1 / 20l4
/abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt /abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt
/abc/def/ghi/Project1/2014/foobar_2014_0916_248.txt /abc/def/ghi/Project1/2014/foobar_2014_0916_248.txt
/abc/def/ghi/Project2 / ABC / DEF / GHI / Project2的
/abc/def/ghi/Project2/2013 / ABC / DEF / GHI / Project2的/ 2013
/abc/def/ghi/Project1/2013/file2_project1.txt /abc/def/ghi/Project1/2013/file2_project1.txt
/abc/def/ghi/Project2/2014 / ABC / DEF / GHI / Project2的/ 2014
/abc/def/ghi/Project2/2014/foobarbaz_2014_0912_255.txt /abc/def/ghi/Project2/2014/foobarbaz_2014_0912_255.txt
/abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt /abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt
Desired Output: 所需输出:
/abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt
/abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt
The following usage of File::Find::Rule
will get you the full list of files. File::Find::Rule
的以下用法将为您提供完整的文件列表。
You can build a hash of arrays to save the results and then filter out the newest file for each project: 您可以构建数组的哈希值以保存结果,然后过滤出每个项目的最新文件:
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;
Outputs: 输出:
(
"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",
)
To finish the filtering, the following addendum uses Path::class
: 为了完成过滤,以下附录使用
Path::class
:
...; # Continued from previous code.
use Path::Class;
my %projects;
for (@files) {
my $file = file($_);
my $project = $file->parent->parent;
$projects{$project} = $file if ! $projects{$project} || $file->stat->mtime > $projects{$project}->stat->mtime;
}
while (my ($project, $file) = each %projects) {
print "$project - $file\n";
}
Outputs: 输出:
testing/project2 - testing/project2/2014/foobarbaz__2014_0916_248.txt
testing/project1 - testing/project1/2014/foobar__2014_0916_248.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.