944,048 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 34628
  • Perl RSS
Apr 13th, 2007
0

parsing multiple files in current directory

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thego!team is offline Offline
1 posts
since Apr 2007
Apr 13th, 2007
0

Re: parsing multiple files in current directory

two ways to go about it:

Perl Syntax (Toggle Plain Text)
  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. }

Perl Syntax (Toggle Plain Text)
  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. }
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Nov 11th, 2009
0

What would be the same for multiple sub directories?

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

Click to Expand / Collapse  Quote originally posted by KevinADC ...
two ways to go about it:

Perl Syntax (Toggle Plain Text)
  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. }

Perl Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thirilog is offline Offline
1 posts
since Nov 2009
Mar 30th, 2010
0

Reading multiple files and pass each file name to another subroutine

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mulunetsi is offline Offline
1 posts
since Mar 2010
Jun 29th, 2010
0

scanning multiple files and sub folders

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Richa Kapur is offline Offline
1 posts
since Jun 2010
Jun 29th, 2010
0
Re: parsing multiple files in current directory
Use this:

Perl Syntax (Toggle Plain Text)
  1. my $dir="/adirectory";
  2. use File::Find;
  3. find(\&subr, $dir);
  4.  
  5. sub subr{
  6. #$_ is set to the filename process here
  7. }
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Jul 6th, 2010
0
Re: parsing multiple files in current directory
To browse through multiple folders to get the files names, pls view this thread.
Reputation Points: 37
Solved Threads: 7
Junior Poster
raul15791 is offline Offline
102 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Unexpected output
Next Thread in Perl Forum Timeline: Use of $ during matching or substitution





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC