簡體   English   中英

Perl File :: Find ::規則排除單個目錄

[英]Perl File::Find::Rule to exclude a single dir

我有以下顯示的目錄結構。

  1. 我想在@project中獲取項目名稱。 @project=('project1','project2') ;
  2. 我想在@project排除OLD目錄及其子目錄
  3. 我想在@project所有項目的子目錄中獲取最新文件。 即,對於project1 ,最新文件位於子目錄2014 ,即foobar__2014_0916_248.txt

如何構建規則來實現這一目標?

use strict;
use File::Find::Rule;
use Data::Dump;
my $output       = "/abc/def/ghi";
my @exclude_dirs = qw(OLD);
my @projects     = File::Find::Rule->directory->in("$output");
dd \@projects;

我的目標結構:

.
├── 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

正如池上建議,只需分兩步完成。

  1. 首先找到您的項目名稱
  2. 第二個找到最新的文件

以下使用Path::ClassPath::Class::Rule

use strict;
use warnings;
use autodie;

use Path::Class;
use Path::Class::Rule;

my $testdir = dir('testing');

for my $project ( $testdir->children ) {
    next if !$project->is_dir() || $project->basename eq 'OLD';

    my $newest;

    my $next = Path::Class::Rule->new->file->iter($project);
    while ( my $file = $next->() ) {
        $newest = $file if !$newest || $file->stat->mtime > $newest->stat->mtime;
    }

    print "$project - $newest\n";
}

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM