Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Getting a list of files in a directory sorted by date

by slloyd (Hermit)
on Apr 07, 2006 at 16:07 UTC ( [id://541893]=perlquestion: print w/replies, xml ) Need Help??

slloyd has asked for the wisdom of the Perl Monks concerning the following question:

How would I get a list of files in a directory, sorted by date? Will the following code insure that the oldest file is first in the list?
opendir(DIR,$logdir); my @cfiles=grep(/\w/,readdir(DIR)); close(DIR);

-------------------------------
Perl - Regularly Expression yourlself
http://www.basgetti.com

Replies are listed 'Best First'.
Re: Getting a list of files in a directory sorted by date
by Ovid (Cardinal) on Apr 07, 2006 at 16:44 UTC

    I'm not sure how expensive pulling the date for a file is, but assuming it's reasonably exensive and you have a lot of files, then a Schwartzian Transform is a natural way to go.

    opendir DIR, $logdir or die "Cannot open $logdir: $!"; my @cfiles = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ -M $_, $_ ] } grep { /\w/ } readdir DIR;

    Update: Fixed the typo that slloyd and philcrow pointed out (/w/ should have been /\w/).

    Cheers,
    Ovid

    New address of my CGI Course.

      The grep expression here looks funny. It seems to say the file must contain a w. Perhaps you meant:
      grep { /\w/ }
      Phil
      Your code example does not return any files.

      -------------------------------
      Perl - Regularly Expression yourlself
      http://www.basgetti.com

Re: Getting a list of files in a directory sorted by date
by explorer (Chaplain) on Apr 07, 2006 at 16:33 UTC
    opendir(DIR,$logdir); foreach( readdir(DIR) ) { next unless /\w/; $date{ -M $_ } = $_; # Store modification's time } closedir DIR; foreach ( sort { $a <=> $b } keys %date ) { print $date{$_} # Show ordered files }

      What happens when you have two files with the same mtime?

      Cheers,
      Ovid

      New address of my CGI Course.

Re: Getting a list of files in a directory sorted by date
by davidrw (Prior) on Apr 07, 2006 at 16:47 UTC
Re: Getting a list of files in a directory sorted by date
by salva (Canon) on Apr 07, 2006 at 21:01 UTC
    use Sort::Key qw(nkeysort); opendir my $dh, "..." or die; my @sorted = nkeysort { -M } grep /\w/, readdir $dh;
Re: Getting a list of files in a directory sorted by date
by kwaping (Priest) on Apr 07, 2006 at 19:37 UTC
    This isn't the most elegant or Perlish solution, but you could just `ls -rt`.

    ---
    It's all fine and dandy until someone has to look at the code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://541893]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2024-04-24 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found