Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File::Find - not traverse directories 1

Status
Not open for further replies.

KevinADC

Technical User
Jan 21, 2005
5,070
US
Is there anyway to make File::Find not traverse through directories but stay in the current directory? The documentation doesn't seem to suggest so and a few things I tried didn't stop File::Find from accomplishing it's mission in life.

Should I just be using readdir() and File::Basename to parse out the filenames and paths instead?
 
If you're not using the recursive nature of File::Find, there's probably no point in trying to force it into being an overcomplicated version of readdir. I'd stick with readdir unless you want fancy matching rules, in which case File::Find::Rule might be of interest (and can be told not to recurse by using the maxdepth() method).
 
I don't think I will need any fancy matching rules, just working on a fairly basic file manager type of script. I really like the way File::Find makes it so easy to work with files in directories. I will look at F::F::R and much appreciate the suggestion ishnid.
 
for posterity, I did find a way to make File::Find not traverse directories. In the preprocess option of File::Find, which like the wanted option is a code reference, you can seperate the files from the directories in the array that preprocess passes to your subroutine, like so:

Code:
find({preprocess => \&sort_em, wanted => \&display_em}, $dir);

sub sort_em {
   my @files = grep(-f,@_);#get only the files
   my @dirs  = grep(-d,@_);#get only the directories
   do whatever here with the files
   &do_something_with(@dirs);
   return(@files);
}

since the sub only returns the array @files back to File::Find, it has nothing to continue traversing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top