954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

parsing multiple files in current directory

Hi,
If it's not too much trouble I would appreciate some help with this - how can I iterate through multiple files in the current directory, opening one after another? For example all files with a name matching students_*.html format. I'm not concerned with files contained in sub-directories.

I've searched for a solution but haven't found anything; if this has already been answered please show me where.

Thanks in advance for your time.

thego!team
Newbie Poster
1 post since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

two ways to go about it:

chdir("path/to/dir") or die "$!";
opendir (DIR, ".") or die "$!";
my @files = grep {/students_.*?\.html/}  readdir DIR;
close DIR;
{
   local @ARGV = @files;
   while(<>){
      #each file will be read line by line here
   }
}
opendir (DIR, "path/to/dir") or die "$!";
my @files = grep {/students_.*?\.html/}  readdir DIR;
close DIR;
foreach my $file (@files) {
   open(FH,"path/to/$file") or die "$!";
   while (<FH>){ 
     #read file line by line here
   }
   close(FH);
}
KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

What would be the same for multiple sub directories?
Thanks in Advance,
Thirilog

two ways to go about it:

chdir("path/to/dir") or die "$!";
opendir (DIR, ".") or die "$!";
my @files = grep {/students_.*?\.html/}  readdir DIR;
close DIR;
{
   local @ARGV = @files;
   while(<>){
      #each file will be read line by line here
   }
}
opendir (DIR, "path/to/dir") or die "$!";
my @files = grep {/students_.*?\.html/}  readdir DIR;
close DIR;
foreach my $file (@files) {
   open(FH,"path/to/$file") or die "$!";
   while (<FH>){ 
     #read file line by line here
   }
   close(FH);
}
thirilog
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Dear,

It was very helpful to see a simplified subroutine to read all files. Could you please help me to return the name of each file and read and open by another subroutine one by one.

mulunetsi
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Hi . This post was very helpful. As per my requirement i need to scan the sub folders also.The above script is ignoring the sub folders. PLease help!!!

thanks,
Richa

Richa Kapur
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Use this:

my $dir="/adirectory";
use File::Find;
find(\&subr, $dir);

sub subr{
#$_ is set to the filename process here
}
mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

To browse through multiple folders to get the files names, pls view this thread .

raul15791
Junior Poster
102 posts since Jun 2008
Reputation Points: 37
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You