Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search a directory in perl

Status
Not open for further replies.

stonemonolith

Programmer
Jul 22, 2006
4
I'm fairly new to perl, so I don't know all the perl syntax. I have several scripts I've written in ksh, and I want to rewrite them in perl, to take advantage of it's speed.

I have a ksh script that searches through a directory to find certain files. The directory has 100's of files, so I have to use find - since we don't know what files to look for until runtime, all of the search criteria are passed in.

Here is the find I use in the ksh script:
list=`find $datadir -type f -name "s_${id}.*${tbl}.*${ext}" -print`

I'm sure there must be a simple answer, but I can't find it. I tried using glob in perl, but I can't get it to work with the variables; I finally figured out the syntax of the perl find command, but again I couldn't get it to work with the variables (only hard-coded search criteria). HELP!

tia
 
Let's see your code? You're using the File::Find module I presume?

Annihilannic.
 
Something like
Code:
die"Can't open $datadir" unless opendir(DIR,$datadir);
my@list=grep{-f&&/^s_$id\..*?$tbl\..*?$ext$/}readdir(DIR);
closedir DIR;
or
Code:
my@list=grep{-f}glob("$datadir/s_$id.*$tbl.*$ext");
With the latter (to be preferred as simpler, but less flexible, no case insensitiveness for instance) the file names are complete with the path.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top