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 | |
|
Re: File:Find DIR search sub routine query
by zentara (Cardinal) on May 27, 2014 at 11:29 UTC | |
by PerlPlay (Novice) on May 29, 2014 at 07:39 UTC |