parsing multiple files in current directory

Reply

Join Date: Apr 2007
Posts: 1
Reputation: thego!team is an unknown quantity at this point 
Solved Threads: 0
thego!team thego!team is offline Offline
Newbie Poster

parsing multiple files in current directory

 
0
  #1
Apr 13th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: parsing multiple files in current directory

 
0
  #2
Apr 13th, 2007
two ways to go about it:

  1. chdir("path/to/dir") or die "$!";
  2. opendir (DIR, ".") or die "$!";
  3. my @files = grep {/students_.*?\.html/} readdir DIR;
  4. close DIR;
  5. {
  6. local @ARGV = @files;
  7. while(<>){
  8. #each file will be read line by line here
  9. }
  10. }

  1. opendir (DIR, "path/to/dir") or die "$!";
  2. my @files = grep {/students_.*?\.html/} readdir DIR;
  3. close DIR;
  4. foreach my $file (@files) {
  5. open(FH,"path/to/$file") or die "$!";
  6. while (<FH>){
  7. #read file line by line here
  8. }
  9. close(FH);
  10. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: thirilog is an unknown quantity at this point 
Solved Threads: 0
thirilog thirilog is offline Offline
Newbie Poster

What would be the same for multiple sub directories?

 
0
  #3
20 Days Ago
What would be the same for multiple sub directories?
Thanks in Advance,
Thirilog

Originally Posted by KevinADC View Post
two ways to go about it:

  1. chdir("path/to/dir") or die "$!";
  2. opendir (DIR, ".") or die "$!";
  3. my @files = grep {/students_.*?\.html/} readdir DIR;
  4. close DIR;
  5. {
  6. local @ARGV = @files;
  7. while(<>){
  8. #each file will be read line by line here
  9. }
  10. }

  1. opendir (DIR, "path/to/dir") or die "$!";
  2. my @files = grep {/students_.*?\.html/} readdir DIR;
  3. close DIR;
  4. foreach my $file (@files) {
  5. open(FH,"path/to/$file") or die "$!";
  6. while (<FH>){
  7. #read file line by line here
  8. }
  9. close(FH);
  10. }
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC