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; } } }

In reply to File:Find DIR search sub routine query by PerlPlay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.