【问题标题】:Perl File::Find::Rule to find latest file in directoriesPerl File::Find::Rule 在目录中查找最新文件
【发布时间】:2014-11-12 02:30:52
【问题描述】:

我正在尝试获取特定路径 ($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:

/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

【问题讨论】:

    标签: perl file find rule


    【解决方案1】:

    File::Find::Rule 的以下用法将为您提供完整的文件列表。

    您可以构建一个数组哈希来保存结果,然后为每个项目过滤掉最新的文件:

    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",
    )
    

    为了完成过滤,下面的附录使用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";
    }
    

    输出:

    testing/project2 - testing/project2/2014/foobarbaz__2014_0916_248.txt
    testing/project1 - testing/project1/2014/foobar__2014_0916_248.txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多