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

Dear PerlMonks,

How can I perform a search on a directory by using only the first part of the directory name?

I am trying to learn this language with a course through a 'toss him into the fire and let him swim' mentality.
I have some experience in VB programming, and have been tasked with updating an existing perl program. I am having a hard time getting around the learning curve.

In my first posting for help it was suggested that I try using globbing to use wildcards. I tried to implement the following:

use File::Glob qw(:globally :nocase); for ($input{"searchtype"} { if (/bid/ | /quot/ | /ezd/) { @dir = bsd_glob("$dir*"); } else { @dir = $dir }


without success... I get the following errors in the error log:

(2)No such file or directory: exec of /var/apache/cgi-bin/cadsearch.cgi failed

client xx.xx.xxx.xx Premature end of script headers: /var/apache/cgi-bin/cadsearch.cgi

More details and code follow.

Any guidance with examples are greatly appreciated.

I need to update a perl program that prints a hyperlink to an autocad drawing. The link will be based on a truck number that the user enters into an html page. That page then posts their entries to this perl script.

This page (cadsearch.cgi) uses another perl page (find_dir_all.pl) that returns a directory to search based on the truck number.

The issue that I am having is that I know only the first part of the directory to search due to inconsistent file naming habits. The first part of the directory will be returned to this program. I need to be able to search for a directory that matches the first part of the directory name.
Example: Joe User searches for truck number 12345. I know that the directory will be something like: BID.12345 Wanna B. Company or Quote.12345 Gonna Geta Truck I know everything up to the end of the number and it will be a unique directory, but I will not know what the customer name added to the end will be. Below is the code that I must update, trimmed down for this posting:
Here is the code that I need to update:
#!/usr/bin/perl ##!/opt/gnu/bin/perl #/usr/local/bin/apache/cgi-bin/cadsearch.cgi #push(@INC,"/cgi-bin"); require "cgi-lib.pl"; use File::Find; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use Socket; &ReadParse(*input); $truck=$input{"truckno"}; #ex: 12345 $searchtype=$input{"searchtype"}; #ex: bid $dir=`/filesystem/scripts/find_dir_all.pl $truck $searchtype`; @dir=("$dir/$truck"); $file=$input{"filename"}; #set variable for new dwg convert web app $dwgpdf="http://webserver/dwgbpdf/"; #variable for hyperlink $hlink # finddepth function to locate directory for ($i=0;$i<=$#dir;$i++){ finddepth(\&dwgWanted,$dir[$i]); } # wanted sub to locate files sub dwgWanted{ $hlink="$File::Find::name" if (/^$file.*$ext2.*/ ); print $hlink }
This is the change that I made:
#Set the default values to be changed by proe/excel/etc $ext=dwg; $ext2=DWG; $truck=$input{"truckno"}; $searchtype=$input{"searchtype"}; $dir=`/depot/systems/scripts/find_dir_all.pl $truck $searchtype`; # use globbing to locate directory based on searchtype and truck numbe +r for ($input{"searchtype"} { if (/bid/ | /quot/ | /ezd/) { @dir = bsd_glob("$dir*"); } else { @dir = $dir } }
This is the perl script called to get the root directory (find_dir_all.pl):
#!/opt/gnu/bin/perl ##!/usr/bin/perl package FindDrw; use Cwd; #This program finds the correct subdirectory for a truck number $truck=$ARGV[0]; $searchtype=$ARGV[1]; #search for sales orders if ($searchtype='so'){ if ($truck >= 900000 ) { $dir = '/directory_tree/so/900000'; } elseif (300000 <= $truck & $truck < 400000) { $dir = '/directory_tree/so/300000/$truck'; } elseif ... } #search for bids elseif ($searchtype='bid'){ if ($truck < 01000){ $dir = '/directory_tree/bids/bid.00900/bid.$truck'; } elseif (01000 <= $truck & $truck < 02000) { $dir = '/directory_tree/bids/bid.01000/bid.$truck'; } elseif ... } #search for quotes elseif ($searchtype='qot'){ if (3000 <= $truck & $truck < 4000) { $dir = '/directory_tree/quotes/quote.3000/Quote.$truck'; } elseif (4000 <= $truck & $truck < 5000) { $dir = '/directory_tree/quotes/quote.4000/Quote.$truck'; } elseif ... } print "$dir";

Replies are listed 'Best First'.
Re: Searching for directory with partial name
by gamache (Friar) on Oct 24, 2007 at 14:32 UTC
    First, I am not sure how the code you supplied runs at all.

    That said, you could probably find the directory you want sorta like this:

    my $n = 12345; my $qdir; for (glob "/some/dir/Quote.${n}*") { if (-d $_) { $qdir = $_; last; } # check that it's a directory } die "Can't find quote directory" unless $qdir;
      How it runs is a mystery to me, too. I will try the example you gave. Thank you very much.