开发者

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

开发者 https://www.devze.com 2023-02-25 08:03 出处:网络
I want to (by using File::Find) first list all fi开发者_如何学Cles in current directory, and after that jump to a subdirectory. Is it possible?Use the preprocess option to do the files in each directo

I want to (by using File::Find) first list all fi开发者_如何学Cles in current directory, and after that jump to a subdirectory. Is it possible?


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 $_ ], @_ }


There is preprocess callback that is called when directory is entered. It can be used for the task like this:

use File::Find;

my $directory = '.';
find({   
    wanted     => sub {
        # do nothing
    }, 
    preprocess => sub { 
        print "$File::Find::dir :\n", join("\n", <*>),"\n\n"; 
        return @_;   # no filtering
    },
}, $directory);

It prints current directory name and list of files within. Note that preprocess is given all directory entries for filtering and they need to be returned.

0

精彩评论

暂无评论...
验证码 换一张
取 消