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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open a file from a directory

Status
Not open for further replies.

cnw40007

Technical User
Mar 4, 2003
35
IE
Hi,
I was wondering could anybody show me how i would open a number of files from a directory.Each file begins with qfh and then a series of different numbers and letters. My code below prints out the specified files but i need to open them to search for a certain word. Can you help me please.

opendir(DIR, "/var/spool/mqueue");
@files = readdir(DIR);

foreach $file (@files)
{
if($file =~ /qfh[0-9a-z]/)
{
print "$file\n";

}
}
closedir(DIR)
 
Try this:

$mydir="/var/spool/mqueue";
opendir(DIR,$mydir);
@files = readdir(DIR);

foreach $file (@files)
{
if($file =~ /qfh[0-9a-z]/)
{
print "$file\n";
$fullname=$mydir.'/'.$file;
open (AFILE, "$fullname");
@array1 = <AFILE>;
foreach $line(@array1) {
<carry out search here>;}
close (AFILE);
}
}
closedir(DIR)
 
That worked thanks very much for your help
cnw40007
 
An even easier method:

use File::Find::Rule;
my @files = File::Find::Rule->file()->name( 'qfh*.*' )->in( &quot;/var/spool/mqueue&quot; );

This will store all the files, with full paths, in the @files array.

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top