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
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
    LVL 26

    Accepted Solution

    Why not simply remove the:

    ->maxdepth( $depth )

    Open in new window

    else  try:  9223372036854775807  (LONG_MAX)

    Author Comment

    @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

    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

    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

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    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…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now