Perl File::Find::Rule to exclude a single dir

╄→尐↘猪︶ㄣ 提交于 2019-12-24 15:53:26

问题


I have the below shown directory structure.

  1. I would like to get just the project names in my @project. @project=('project1','project2');
  2. I would like to exclude OLD directory and its sub directories in @project
  3. I would like to get latest file in the subdirectories for all projects in @project. i.e., for project1, latest file is in sub directory 2014, which is foobar__2014_0916_248.txt

How can I frame a rule to achieve this?

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;

My Dir Structure:

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


As ikegami suggested, just do this in two steps.

  1. First find your project names
  2. Second find the latest file

The following does this using Path::Class and Path::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";
}

Outputs:

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


来源:https://stackoverflow.com/questions/25895751/perl-filefindrule-to-exclude-a-single-dir

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