Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File timestamp

Status
Not open for further replies.

whegge

IS-IT--Management
Jan 2, 2002
40
0
0
US
How do I get the most recent file from directory? I am trying to automate a log analyzer program. The log file names are very cryptic, qmail log files if you wish to know, so I cannot get the proper file by name; so, I want to find it by timestamp. Thanks in advance for the help.

Wes
 
Code:
stat()
returns a lot of info about a file given either a path or a handle.

The most recent file in a directory would be given by
Code:
sub latest {
        my $dir = $_[0];
        opendir( DIR, $dir ) or die "$0: opendir $dir: $!";
        my( $time, $file );
        while( $_ = readdir(DIR) ) {
             next if ( /\.{1,2}/ ); # avoid . & ..
             my $tstamp = ( stat($_) )[9]; # mtime, 8=atime, 10=ctime
             next unless $time < $tstamp;
             $file = $_;
             $time= $tstamp;
        }
        return $file;
}

Good luck,



fish

P.S. Unless you really like qmail, have a look at exim.

&quot;As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilk
 
fishiface's regex to skip &quot;.&quot; and &quot;..&quot; dir files is broken. It skips ANY files that has a &quot;.&quot; or &quot;..&quot; in it, effectively eliminating most files and not just the dir files. The regex needs anchors; /^\.\.?$/

sub latest ($) {
my $dir = shift;
opendir DIR, $dir or die &quot;$0: Can't opendir $dir: $!&quot;;
my($time, $file);
for (readdir(DIR)) {
next if /^\.\.?$/;
next if (my $tstamp = (stat$_)[9]) < $time;
$file = $_;
$time= $tstamp;
}
return $file;
}
 
Thanks for the quick responses. I will try it out this evening. Would love to do it know but a server is down - priorities.

Thanks again,
Wes Hegge
SignalBlast.Com, Inc.
 
Finally got a chance to try the function. It seems to be working but not giving me what I need. What is happening is the log file is named @4000... (tai64n date stamp). When I do an 'ls -l' I can see the file and it has a date associated with it. When I run the program and ask it to print out the date it does not show one. Here is how I have to code right now:

#!/usr/bin/perl

$latestfile = latest(&quot;/var/log/qmail&quot;);

print &quot;$latestfile\n&quot;;

sub latest {
my $dir = $_[0];
opendir(DIR, $dir) or die &quot;$0: opendir $dir: $!&quot;;
my($time, $file);
while ($_ = readdir(DIR) ) {
next if ( /^\.\.?$/ );
my $tstamp = ( stat($_) )[9];
print &quot;$_\t$tstamp\n&quot;;
}
return $file;
}

What I get is a list of filenames but no timestamp. I have tried atime, mtime and ctime with 'stat'. They all return no timestamp. Am I missing something simple here?
 
r u on a windoze system?
Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Code:
#!/usr/bin/perl

$latestfile = latest(&quot;/var/log/qmail&quot;);

print &quot;$latestfile\n&quot;;

sub latest {
    my $dir = $_[0];
    opendir(DIR, $dir) or die &quot;$0: opendir $dir: $!&quot;;
    my($time, $file);
    while ($_ = readdir(DIR) ) {
        next if ( /^\.\.?$/ );        
        #my $tstamp = ( stat($_) )[9];     
        my @filedata = stat($_);
        $tstamp = $filedata[9];
        print &quot;$_\t$tstamp\n&quot;;
    }
    return $file;
}

This is giving a time string, but it won't return the file name $file, because its not being assigned.

It's only giving the timestring for three files, what's your regex matching on?

HTH
Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Scratch that,
this is working
#!/usr/bin/perl

$latestfile = latest(&quot;/home/httpd/cgi-bin&quot;);

print &quot;$latestfile\n&quot;;

sub latest {
my $dir = $_[0];
opendir(DIR, $dir) or die &quot;$0: opendir $dir: $!&quot;;
my($time, $file);
while ($_ = readdir(DIR) ) {
next if ( /^\.\.?$/ );
#my $tstamp = ( stat($_) )[9];
my @filedata = stat;$tstamp = $filedata[9];#now a tidy one liner ;)
print &quot;$_\t$tstamp\n&quot;;
}
return $file;
}
HTH
Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top