• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

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,
0
Tolgar
Asked:
Tolgar
  • 2
2 Solutions
 
arober11Commented:
Why not simply remove the:

->maxdepth( $depth )

Open in new window


else  try:  9223372036854775807  (LONG_MAX)
0
 
TolgarAuthor Commented:
@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,
0
 
tdlewisCommented:
When you specify maxdepth, you're saying that you want it to stop at that depth; the only way to avoid stopping is to have a value that is so large you cannot possible hit it. (LONG_MAX seems a little excessive to me, but any big value should work; I would have gone with something like 1024 :-)
0
 
arober11Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now