• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 773
  • Last Modified:

Find newest file in directory and then search it

I have a batch job that i run about 3 to 4 times a day. It creates a log of format 1262002-1000 am.log i want to find the newest log file and then grep its results. I have the grep part written but am struggling with how to do the opendir and use a array to find the newest file and then run my grep against that.
0
gdmacmillan
Asked:
gdmacmillan
  • 5
  • 4
2 Solutions
 
fim32Commented:
why use perl?

if you already have your grep:

grep "searchstring" `ls -tr | tail -1`

tho, if you wanted, you could put that backtick command into perl, too...
0
 
gdmacmillanAuthor Commented:
Sorry forgot to mention im doing this on a windows system thats why im using perl.
0
 
jmcgOwnerCommented:
We have been asked something very much like this before:

http://www.experts-exchange.com/Q_20845254.html

For your case, I think it might work if you started out with something like the following:

use strict;
use File::stat;

my $newest = [ undef, 0 ];

for ( <*.log> ) {
    my $st = stat($_) or next; # should warn here?
    my $mtime = $st->mtime;

    $newest = [ $_, $mtime] if $mtime > newest->[1] ;
    }

print "most recent file is: ", $newest->[0], "\n";

This should be reasonably efficient and I think I've avoided the typos that plagued that other question. With $newest->[0] in hand as your filename, you can then apply whatever grep code you had already worked out.



0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
fim32Commented:
ugh.

and your log name leaves a little to be desired... is your example from jan 26th? or from dec 6th?

you could scan the filenames into an array and use sort, but only after you did some magic on the names. specifically, you need to alter all the filenames so they can be compared... like using YYYYMMDDHHMM.log instead of what you got with the am's and all...
0
 
fim32Commented:
or, use File::stat as jmcg suggests :P
0
 
gdmacmillanAuthor Commented:
Hi Jcmp,
          yep ive been looking at those other awnsers but i am quite a newbie to perl. The last programming i did was pascal back in college in 90.  I understand we are creating a array which is undefined and then searching through the files to find the newest.  The stat function is used to get info from the flie. What i get stuck at is how do i tell it which directory to go search.
0
 
fim32Commented:
that's in the for part:

for ( <.*log> ) {

the <.*log> part says search the local directory for all files names .*log, it's a shortcut to using the opendir and such.
0
 
gdmacmillanAuthor Commented:
Doh. So if i run it from that directory it will search that directory? What if i wanted to use it to go search another one?
0
 
fim32Commented:
you could as easily wrap that for loop in a opendir/closedir:

opendir D,"/newdir";
for (readdir D) {
  other stuffs here
}
closedir D;

or, if you wanted to make sure it was only *log files you were getting, change the abovre for (readdir) to:
for (grep { /.*log/ } readdir D) {
0
 
gdmacmillanAuthor Commented:
Hi fim32 & Jmcg thanks for all your help on this. I have the program working now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now