Instead of depending on the file name to tell you how old it is, maybe you could use the age of the file as given by the "-M" function (read about it via perlfunc -f -X.

Since the value returned by -M is in days, you have to know that 1 minute equals 0.0006944 days:

#!/usr/bin/perl use strict; my $Usage = "Usage: $0 pathname\n"; die $Usage unless ( @ARGV == 1 and -d $ARGV[0] ); my $dir = shift; opendir( DIR, $dir ) or die "opendir $dir failed: $!\n"; my @newfiles = grep { -f "$dir/$_" && -M _ < 0.000694445 } readdir DIR +; printf( "Found %d new files at %s\n", scalar @newfiles, scalar localti +me ); print " " , join( "\n ", @newfiles ) . "\n" if ( @newfiles );
(updated to be a little more accurate about the age limit -- .0007 was probably too loose.)

Now, if there happens to a file that was created an hour (day, week, month, year) ago and was modified within the last minute (i.e. its content changed, by appending or altering data), it will be reported as "new" regardless of its name, along with any file that was actually created within the last minute. If you don't want that, then you'll need to fall back to checking the file names.

(I tried the "-C" function as well, but on my macosx/BSD system, it behaved the same as "-M" -- still, you might test it under solaris to see if "inode change time" is different in some useful way from "modification time".)

Note that -M reports file age relative to the time at which your script began running, so the length of time it takes to run the script has no effect on the results (the last file fetched from the directory will be judged by the same criterion as the first file, no matter how many files are in the directory).


In reply to Re: Finding Last minute file in a directory by graff
in thread Finding Last minute file in a directory by John007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.