Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

What value should I assign to maxdepth if I want to search to the deepest point of the directory structure in Perl?

Hi,
I use the following code to find files in a directory and its subdirectories:

sub findFiles(){
my ($fileType, $depth, $directory) = @_;
	my @files = File::Find::Rule->file()
				    ->name( "*.$fileType" )
				    ->maxdepth( $depth )
				    ->in( $directory );
return (\@files);

Open in new window


What value should I assign to $depth if I want to search to the deepest point of the directory structure?

My temporary solution is to assign a very high value but I'd like to make it more robust.

I tried 0 but it didn't work.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of arober11
arober11
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar
Tolgar

ASKER

@arober11: because I also have another option where I use the depth. Therefore, I'd like to keep the depth in the code and make it infinite if it is not defined.

As I see, you recommend me to put a very large number.

Is there any other way of doing it other than assigning a hard coded number.

Thanks,
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alternitively rewrite the function, to validate the passed arguments and only add a maxdepth, if required e.g.

sub findFiles(){
my ($fileType, $depth, $directory) = @_;
my($rule, @files);

$rule =  File::Find::Rule->new;
$rule->file;
$rule->name( "*.$fileType")        if $fileType ;
$rule->maxdepth( $depth)          if ($depth  &&  $depth > 0) ;
@files = $rule->in( $directory )   if $directory;

return (\@files);

Open in new window