Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Find most recently file

by kyle (Abbot)
on Mar 31, 2009 at 14:49 UTC ( #754440=note: print w/ replies, xml ) Need Help??


in reply to Find most recently file

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.


Comment on Re: Find most recently file
Select or Download Code
Re^2: Find most recently file
by clementine (Initiate) on Mar 31, 2009 at 16:49 UTC
    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é !!"; } }

      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.

        The sigil isn't missing in the context of the given code snippet - ...If stat is passed the special filehandle consisting of an underline, no stat is done, but the current contents of the stat structure from the last stat, lstat, or filetest are returned. ... - from stat

        A user level that continues to overstate my experience :-))

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://754440]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-09-16 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (13 votes), past polls