3

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

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
marverix
  • 7,184
  • 6
  • 38
  • 50

2 Answers2

8

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 $_ ], @_ }
ysth
  • 96,171
  • 6
  • 121
  • 214
1

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.

bvr
  • 9,687
  • 22
  • 28