PerlPlay has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks

I have the following code looking for a given dir name eg P12345678 it does return the path if found but the problem I'm facing is that it doesn't find it and end the sub routine it just continues, the eventual location I'll be searching in once I get this program to work in the test location contains 10's of thousands of directories.

Once I get this working (With help from the mighty Monks), I'll then try to modify the code to get it to search for numerous DIR's via a .csv file

#!/usr/bin/perl #FindDir #Usage example - perl finddir P12345678 #Usage example - perl finddir D12345678 package finddir; our $package = 'FindDir'; use strict; use warnings; use File::Find qw(find); #*****************Path Variables************************ our @RDDSpaths = ('C://Temp/hddzip/', ); our @NASpaths = ('C://Temp/Alcohol/', ); #******************************************************* #Die if no parameters are given to the program @ARGV or die "$package: no parameters given\n"; #Checking for valid dir name our $DIR=shift; chomp $DIR; $DIR =~qr{^[LIDMP]\d{8}$} or die "$package: $DIR is not a valid Media +Identifier\n"; #Set variables our $found = 0; our $location = 0; #If DIR starts with either I,D or M and has another 8 characters proce +ss if ($DIR =~qr{^[IDM]\d{8}$}) { print "Searching for $DIR on NAS\n"; #Search NAS paths find(\&wantedNAS, @NASpaths); $found or print "Unable to locate $DIR in path"; if ($found != 0) { print "Found $DIR in $location\n"; } sub wantedNAS { return unless -d; $File::Find::prune = 1 if /[IDM]\d{8}$/; # Don't recurse. print "$File::Find::dir/$_/\n" if /[IDM]\d{8}$/; if ($_ eq $DIR){ #Store path location of DIR if found $location = $File::Find::name; $found++; #exit; } } } #Else If DIR starts with either L or P and has another 8 characters pr +ocess else { print "Searching for $DIR on RDDS\n"; find(\&wantedRDDS, @RDDSpaths); $found or print "Unable to locate $DIR in path"; if ($found != 0) { print "Found $DIR in $location\n"; } sub wantedRDDS { return unless -d; $File::Find::prune = 1 if /[LP]\d{8}$/; # Don't recurse. print "$File::Find::dir/$_/\n" if /[LP]\d{8}$/; if ($_ eq $DIR){ #Store path location of DIR if found $location = $File::Find::name; $found++; #exit; } } }

Replies are listed 'Best First'.
Re: File:Find DIR search sub routine query
by wjw (Priest) on May 27, 2014 at 06:44 UTC

    If possible, take a look at File::Find::Rule I think you might find it less work to get what you want out of it.

    ...the majority is always wrong, and always the last to know about it...

    Insanity: Doing the same thing over and over again and expecting different results...

    A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is a facct

Re: File:Find DIR search sub routine query
by zentara (Cardinal) on May 27, 2014 at 11:29 UTC
    I havn't tested this with your particular code, but I remember having a similar problem, and it was solved like this, by adding some explicit returns:
    # instead of your if, split it up into 2 lines # return unless -d; # $File::Find::prune = 1 if /[LP]\d{8}$/; # Don't recurse. # print "$File::Find::dir/$_/\n" if /[LP]\d{8}$/; return unless -d; return unless $_ =~ /[LP]\d{8}$/; $File::Find::prune = 1; # don't descend print "$File::Find::dir\n"; print "$file::Find::name\n"; # or possibly better return $File::Find::prune = 1 if /[LP]\d{8}$/; # Don't recurse.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thanks for responding, I tried what you said but it still didn't work for me. I'll continue to fiddle and see if I can find a working solution.