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.

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

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

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.

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

Use this:

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

sub subr{
#$_ is set to the filename process here
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.