How can I use File::Find in Perl?

I'm a bit confused from File::Find documentation... What is the equivalent to $ find my_dir -maxdepth 2 -name "*.txt"?


Personally, I prefer File::Find::Rule as this doesn't need you to create callback routines.

use strict;
use Data::Dumper;
use File::Find::Rule;

my $dir = shift;
my $level = shift // 2;

my @files = File::Find::Rule->file()

print Dumper(\@files);

Or alternatively create an iterator:

my $ffr_obj = File::Find::Rule->file()

while (my $file = $ffr_obj->match())
    print "$file\n"

I think I'd just use a glob since you really don't need all the directory traversal stuff:

 my @files = glob( '*.txt */*.txt' );

I made File::Find::Closures to make it easy for you to create the callbacks that you pass to find:

 use File::Find::Closures qw( find_by_regex );
 use File::Find qw( find );

 my( $wanted, $reporter ) = File::Find::Closures::find_by_regex( qr/\.txt\z/ );

 find( $wanted, @dirs );

 my @files = $reporter->();

Normally, you can turn a find(1) command into a Perl program with find2perl (removed in v5.20 but on CPAN):

% find2perl my_dir -d 2  -name "*.txt"

But apparently find2perl doesn't understand -maxdepth, so you could leave that off:

% find2perl my_dir -name "*.txt"
#! /usr/local/perls/perl-5.13.5/bin/perl5.13.5 -w
    eval 'exec /usr/local/perls/perl-5.13.5/bin/perl5.13.5 -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell

use strict;
use File::Find ();

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;

# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, 'my_dir');

sub wanted {
    && print("$name\n");

Now that you have the starting programming, you can plug in whatever else you need, including a preprocess step to prune the tree.

use File::Find ; 
use Cwd ; 

my $currentWorkingDir = getcwd;

my @filesToRun = ();
my $filePattern = '*.cmd' ; 
#add only files of type filePattern recursively from the $currentWorkingDir
find( sub { push @filesToRun, $File::Find::name  
                                    if ( m/^(.*)$filePattern$/ ) }, $currentWorkingDir) ;

foreach  my $file ( @filesToRun  ) 
    print "$file\n" ;   

There's also the handy find2perl utility. Use it instead of the Unix find command, with the same command-line arguments as 'find', and it will generate the corresponding Perl code that makes use of File::Find.

$ find2perl my_dir -maxdepth 2 -name "*.txt"

Need Your Help

Getting the ID of the element that fired an event

javascript jquery

Is there any way to get the ID of the element that fires an event?

Posting JSON using JQuery and to set HTTP content type - 'application /json'

ajax json jquery http-post

I am using jquery to post Json data to server. However when I make a post request as below,