I'm not sure you can get one solution for this due to differing filesystems on differing platforms. Even on *Nix platforms you cannot not reliably do this (depending on your definition of oldest and file). I've always found it easier to control file naming but ... this snippet might help (at least on *Nix).

#!/usr/bin/perl use IO::Dir; tie %dir, 'IO::Dir', "."; # assume ctime is close enough for creation time foreach (sort { $dir{$b}->ctime <=> $dir{$a}->ctime } keys %dir ) { print $_, " " , $dir{$_}->ctime,"\n"; }

-derby

update: Of course jweed got what I was talking about (admittedly in a round-a-bout way). The OP wanted the oldest created file. In *Nix world, you cannot do that. There are three times associated with a file - last access, last modified and last change time. The -M solutions that follow will work as long as your concept of modification and creation are the same. I can create a file on Jan 1st, another on June 1. If I modify the Jan 1st file (and possibly in some non-significant manner) on Aug 1, then by the -M method, the Jan 1 file will be newer than the June 1 file. See - it all depends on how you define oldest. If your files are created but never modified (such as a caching scheme) then the -M works fine; however, if there is a chance that the files will be modified and you don't count the modifications as making the file newer then checking the ctime is probably better. But then again, that can have issues to ... so that's why I normally suggest a naming convention (date/time) to remove ambiguity.


In reply to Re: Quickest way to get the oldest file by derby
in thread Quickest way to get the oldest file by Anonymous Monk

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.