how to open several log files

perlyhead
perlyhead used Ask the Experts™
on
What's the best way of opening several log files to be read, preferably just read all the files in a directory
c:\logfiles\*.log

open(LOG,"<log1.log") or die("Unable to open the log file!\n

while{
log1
....}

without having to name log1, log2, log3, etc..
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
foreach my $logfile (glob("*.log")) {
  unless(open(LOG,$logfile)) {
    warn "can't access $logfile - $!";
    next;
  }
  while(<LOG>) {
    # do something
  }
  close(LOG);
}

Author

Commented:
thanks, that works perfectly. one more thing... how do i specify a directory to read from?
Commented:
foreach my $logfile (glob("c:\\logfiles\\*.log"))

:-)
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
{local @ARGV=<c:/logfiles/*.log>;
 while( <> ){
     print $_;
 }
}

Author

Commented:
Smashing! If there are no files to be read it doesn't print a message. Is there something like this I can use?:

or die ("unable to open log file")

Author

Commented:
it's ok, i got it, thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial