简体   繁体   English

使用Perl模块File :: Find :: Rule查找匹配的文件

[英]Finding matching files with Perl module File::Find::Rule

I'n trying to use the File::Find::Rule module to find a specific file ( output.txt ) in a subdirectory, and if it is not there then search in the root directory to see if it exists. 我正在尝试使用File::Find::Rule模块在子目录中查找特定文件( output.txt ),如果没有,请在根目录中搜索以查看是否存在。 The issue is that multiple output.txt files exist, so we should only be looking for others if the original is not found. 问题是存在多个output.txt文件,因此,如果找不到原始文件,我们应该只寻找其他文件。

Basically the directory structure looks like this 基本上目录结构是这样的

top
    level-1-a
        level-2-a
            output.txt
        level-2-b
            output.txt
    level-1-b
        level-2-a
            output.txt
        level-2-b
            output.txt

Right now I have: 现在我有:

    @files = File::Find::Rule->file()->name($output)->in($sub_dir);

    if ( ! @files ) {
        @files = File::Find::Rule->file()->name($output)->in($root_dir);
    }

Where the behavior is, we look for output.txt in \\top\\level-1-a first, where it finds the matches in level-2-a and level-2-b . 行为所在的地方,我们首先在\\top\\level-1-a查找output.txt,然后在level-2-alevel-2-b找到匹配项。 If there are no matching files under level-1-a , we will then make the same call on \\top to find the matches show up in the level-1-b directories. 如果在level-1-a下没有匹配的文件,我们将在\\top上进行相同的调用以查找显示在level-1-b目录中的匹配项。 Is there a cleaner way to check with that "if-else" idea? 有没有更干净的方法可以验证“ if-else”的想法?

I would check the subdirectories one at a time. 我会一次检查一个子目录。 Here's an example. 这是一个例子。 The last breaks out of the for loop as soon as soon as a subdirectory has been found that contains the required file 一旦找到包含所需文件的子目录, last就会脱离for循环

my @files;

for my $subdir ( 'level-1-a', 'level-1-b' ) {
    last if @files = File::Find::Rule->file()->name($output)->in("/top/$subdir");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM