in reply to Quickest way to get the oldest file

Sometimes, the first file in the directory is the oldest one:
my $file; opendir(DIR, $dir) or die "Can't read $dir: $!"; $file = readdir(DIR) until $file =~ /^[^.]/; print "First file is $file\n"; closedir DIR;

This should be pretty fast and memory efficient, since it doesn't actually read the whole directory. Of course, it won't work if the files in the directory can be modified after being placed there, as the oldest file will no longer be the first.

-- zigdon

Replies are listed 'Best First'.
Re: (z) Re: Quickest way to get the oldest file
by mr_mischief (Monsignor) on Jan 23, 2004 at 15:00 UTC
    Sometimes is right. The code should work on some filesystems, but it's dangerous to make any assumptions of this kind. One would need to know the documented behavior of a filesystem for this to be trusted, especially so across different implementations or versions of a filesystem as any undocumented behavior is apt to change.

    Still, in the right circumstances, it's probably going to be the fastest option.



    Christopher E. Stith