Link to home
Start Free TrialLog in
Avatar of perlyhead
perlyhead

asked on

how to open several log files

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..
Avatar of thoellri
thoellri

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

ASKER

thanks, that works perfectly. one more thing... how do i specify a directory to read from?
ASKER CERTIFIED SOLUTION
Avatar of thoellri
thoellri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
{local @ARGV=<c:/logfiles/*.log>;
 while( <> ){
     print $_;
 }
}
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")
it's ok, i got it, thanks!