Search code examples
perlfile-find

Perl File::Find: First list all files in directory and then jump to next dir?


I want to (by using File::Find) first list all files in current directory, and after that jump to a subdirectory. Is it possible?


Solution

  • Use the preprocess option to do the files in each directory before descending into subdirectories:

    use strict;
    use warnings;
    use File::Find 'find';
    
    find(
        {
            'wanted' => sub { print "$File::Find::name\n" },
            'preprocess' => sub { sort { -d $a <=> -d $b } @_ }
        },
        '.'
    );
    

    Though to avoid extra stats, it should be:

    sub { map $_->[0], sort { $a->[1] <=> $b->[1] } map [ $_, -d $_ ], @_ }