<[EMAIL PROTECTED]> wrote:
>
> I am trying to write some code to read the most recent log file in a
> directory. I wrote some code below. This works but I was wondering if
> there was a more efficient method to do this. Ideally I would like to
> include hours, minutes and seconds but that's not necessary at this
> point.
>
> foreach $iislog (@files) {
>
>   ($WRITETIME) = (stat("$iislogs\\$iislog"))[9];
>
>   print scalar localtime ($WRITETIME);
>
>   ($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday, $isdst) = 
> localtime();
>   ($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2, $yday2, $isdst2) = 
> localtime($WRITETIME);
>
>   if ($day == $day2 && $month == $month2) {
>
>     print "\n\n";
>     print "The file was last modified on: ";
>     print scalar localtime ($WRITETIME);
>     print "\n\n";
>   }
> }

Hi Paul.

First of all,

  use strict;   # And declare all of your variables
  use warnings;

# And indent your code!

Then I'm not sure what you need. You say you want to read the most recent log file,
but your code just prints out a list of modification times. Do you need this
as well,or do you just want to find the latest file?

  (stat $file)[9]

gives you the modification date, while

  -M $file

gives you the age of the file. So you could just write

  @files = sort { -M $a <=> -M $b } @files;
  print $files[-1], "\n";

Or do you need anything more?

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to