in reply to Quickest way to get the oldest file

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.

Replies are listed 'Best First'.
Re: Re: Quickest way to get the oldest file
by jweed (Chaplain) on Jan 19, 2004 at 21:51 UTC
    Even on *Nix platforms you cannot not reliably do this
    To clarify: (from the unix-faq):
    3.1) How do I find the creation time of a file? You can't - it isn't stored anywhere. Files have a last-modified time (shown by "ls -l"), a last-accessed time (shown by "ls -lu") and an inode change time (shown by "ls -lc"). The latter is often referred to as the "creation time" - even in some man pages - but that's wrong; it's also set by such operations as mv, ln, chmod, chown and chgrp. The man page for "stat(2)" discusses this.



    Code is (almost) always untested.
    http://www.justicepoetic.net/