Perl File :: Find :: Rule排除单个目录 [英] Perl File::Find::Rule to exclude a single dir

查看:120
本文介绍了Perl File :: Find :: Rule排除单个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下所示的目录结构.

I have the below shown directory structure.

  1. 我只想在我的@project中获得项目名称. @project=('project1','project2');
  2. 我想在@project
  3. 中排除OLD目录及其子目录
  4. 我想在@project中所有项目的子目录中获取最新文件.即,对于project1,最新文件位于子目录2014中,该子目录为foobar__2014_0916_248.txt
  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;

我的目录结构:

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

推荐答案

正如池上所建议的那样,只需分两个步骤进行操作即可.

As ikegami suggested, just do this in two steps.

  1. 首先找到您的项目名称
  2. 第二次找到最新文件

以下使用 Path::Class

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";
}

输出:

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

这篇关于Perl File :: Find :: Rule排除单个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆