Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Find most recently file

by clementine (Initiate)
on Mar 31, 2009 at 14:06 UTC ( [id://754431]=perlquestion: print w/replies, xml ) Need Help??

clementine has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am a newby in perl. How can I do to find the most recently file log (beginning by "log") in a windows directory ? Thanks for your help !

Replies are listed 'Best First'.
Re: Find most recently file
by kennethk (Abbot) on Mar 31, 2009 at 14:31 UTC

    Welcome to the Perl community, clementine.

    The stat command will return, among a number of other things, the creation time of a file (on Windows only) and the modification time of a file (see porting information for details). You can get directory contents using opendir, readdir and closedir. And you can compare the strings using either substr and Equality Operators or, if you are feeling more adventurous, regular expressions. I'll be happy to take a look at any code you cobble together to give more specific advice.

Re: Find most recently file
by kyle (Abbot) on Mar 31, 2009 at 14:49 UTC

    In addition to the excellent pointers you've gotten from kennethk, I'd add that the -M file test operator might be useful here. It does a stat, but it only returns modification time instead of all the other stuff that stat does. Since the time that -M gives you is relative to the time your script started, it's not very good for finding an absolute time, but it's fine for doing comparisons, which is what you're after.

      ok thanks. I tried with 'stat' but it doesn't not work. use warnings; use strict; use File::Find; my ($recent,$nom,$rep); my $line; $rep = 'D:\\'; # cherche le plus recent sub plus_recent { return unless -f; return if defined $recent and $recent > (stat($_))9; $recent = (stat(_))9; $nom = $File::Find::name; } find(\&plus_recent,$rep); print $nom; #open (LOG,"<D:\\log.log") || die ("Erreur d'ouverture de TOTO") ; open(LOG,"$nom") ; my @LogFilePosition=<LOG>; close(LOG); foreach $line (@LogFilePosition) { if ($line =~ /CHAINERECHERCHEE/) { print "C'est démarré !!"; } }

        For our other readers, this is a more legible version.

        use warnings; use strict; use File::Find; my ($recent,$nom,$rep); my $line; $rep = 'D:\\'; sub plus_recent { return unless -f; return if defined $recent and $recent > (stat($_))[9]; $recent = (stat(_))[9]; $nom = $File::Find::name; } find(\&plus_recent,$rep); print $nom; #open (LOG,"<D:\\log.log") || die ("Erreur d'ouverture de TOTO") ; open(LOG,"$nom") ; my @LogFilePosition=<LOG>; close(LOG); foreach $line (@LogFilePosition) { if ($line =~ /CHAINERECHERCHEE/) { print "C'est démarré !!"; } }

        The only thing I have to add to the other advice you've gotten is that you might want to add a newline to your print there.

        print $nom, "\n";

        Now that I look a little more, you might want to check that your open succeeded as you do with the one you apparently commented out. I'd write it this way:

        open my $log_fh, '<', $nom or die "Can't read '$nom': $!";

        That way you get a clear error message if it fails.

        Please read Writeup Formatting Tips - in particular, surrounding your code with <code> tags will make it dramatically more legible. In the future, you should also include your output and a better description of your problem - saying "but it does not work" is usually frowned upon since there are many ways a code can "not work".

        In your code, note that the line $recent = (stat(_))[9]; is missing a sigil in front of $_ - you mean $recent = (stat($_))[9];. The big problem you are encountering is likely that you are not testing your file name in your plus_recent subroutine to see if the file is a log file - you are locating the most recent file in your entire directory tree (after the $_ fix).

        Note also in your commented open line you should likely use or in place of || - see Operator Precedence and Associativity.

        Update: Learned something new re: stat. Thanks Bloodnok.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://754431]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-18 02:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found