简体   繁体   中英

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. The issue is that multiple output.txt files exist, so we should only be looking for others if the original is not found.

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 . 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. Is there a cleaner way to check with that "if-else" idea?

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

my @files;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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