Perl File::Find::Rule to find latest file in directories

余生颓废 提交于 2020-01-16 04:14:06

问题


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

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/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

Desired Output:

/abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt
/abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt

回答1:


The following usage of File::Find::Rule will get you the full list of files.

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:

...; # 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


来源:https://stackoverflow.com/questions/25900989/perl-filefindrule-to-find-latest-file-in-directories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!