[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2012-08-28
Medium Priority
Last Modified: 2012-09-07
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.

Question by:Tolgar
  • 2
LVL 26

Accepted Solution

arober11 earned 1000 total points
ID: 38342579
Why not simply remove the:

->maxdepth( $depth )

Open in new window

else  try:  9223372036854775807  (LONG_MAX)

Author Comment

ID: 38342674
@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.

LVL 10

Assisted Solution

tdlewis earned 1000 total points
ID: 38342934
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 :-)
LVL 26

Expert Comment

ID: 38343776
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->name( "*.$fileType")        if $fileType ;
$rule->maxdepth( $depth)          if ($depth  &&  $depth > 0) ;
@files = $rule->in( $directory )   if $directory;

return (\@files);

Open in new window


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question