Controlling traversal depth using File::Find

M

Mark

I am using File::Find to process files in a specified directory tree.
I would like to be have find() only traverse a top level directory
(controlling how deep to go would also be helpful).

This is the best I could do. This code breaks if a relative directory
path is specified. It seems that File::Find should provide a way to
specify the maximum depth but I could not find anything.

use strict ;
use warnings;
use File::Find;

$| = 1;

my $TopDir = '/temp/sub';
find({wanted => \&wanted},$TopDir);

sub wanted {
if (-d $File::Find::name && $File::Find::name ne $TopDir) {
$File::Find::prune = 1 ;
return;
}

return if -d;

# Processing for the file goes here
print "file: $File::Find::name\n";
}
 
R

Randal L. Schwartz

Mark> I am using File::Find to process files in a specified directory tree.
Mark> I would like to be have find() only traverse a top level directory

Why not just glob it then?

The whole point of File::Find is because glob is only one level!
 
M

Mark

Why not just glob it then?
The whole point of File::Find is because glob is only one level!

The user will specify, as an input parameter, how deep to traverse a
directory tree. I was hoping to be able to use File::Find for all
depths, including the depth of 0. I suppose I could treat depth 0 as
a special condition and use glob in that case but I would like to
avoid that if File::Find is capable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top