http://www.perlmonks.org?node_id=1000174

coltman has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,

I have several hundreds of txt files to read. Unfortunately, they are stored under multiple sub-folders (could be several layers). How can I let PERL automatically search and read these files?

I have the following code. However, (1) it does not read through sub-folders and (2) it works only when I place the perl code under the same folder with the txt files. How can I modify my code to (1) read both root and sub-folders? and (2) specify the root folder rather than the default "dir"?

Any widsom will be greatly appreciated!

#=====file reading=============; open(FIND, "dir *.txt /B |") || die "couldn't open: $!\n"; #============Text reading In Each File====================; FILE: while (<FIND>) { m/(20\d\d)/; $filename=$1; print "$filename\n"; if (!open(TEXTFILE, $_)) { #print $_; print "Can't open $_--continuing...\n"; next FILE; } #============Article reading======================; while(<TEXTFILE>) { ......................................