glob a directory then sort by timestamp

Discussion in 'Perl Misc' started by jhellma1@gmail.com, Feb 22, 2008.

  1. Guest

    All,

    I am trying to glob a directory, storing the filenames into an array.
    The trick is that I then want to sort that array by the files'
    timestamps. Is there an easy way to do this?

    TIA!
     
    , Feb 22, 2008
    #1
    1. Advertisements

  2. wrote:
    >I am trying to glob a directory, storing the filenames into an array.
    >The trick is that I then want to sort that array by the files'
    >timestamps. Is there an easy way to do this?


    Looping through and stat()ing hundreds or thousands of files individually is
    going to be slow. Depending upon _which_ timestamp (created, accessed,
    modified, ...) you want to sort by it may be significantly faster to call an
    external program (e.g. ls) with the proper parameters and simply capture the
    output.

    jue
     
    Jürgen Exner, Feb 22, 2008
    #2
    1. Advertisements

  3. Tony Curtis Guest

    wrote:
    > All,
    >
    > I am trying to glob a directory, storing the filenames into an array.
    > The trick is that I then want to sort that array by the files'
    > timestamps. Is there an easy way to do this?


    The glob() is going to do its own sort, so that would be wasteful.

    Better to opendir(), grep() a readdir() for the required items, closedir()
    and then sort those by stat()ing for {modified,changed,accessed} time.

    Don't forget to see "perldoc -f stat" about the use of "_" to minimize
    file system hits.

    hth
    t
     
    Tony Curtis, Feb 22, 2008
    #3
  4. wrote:
    > All,
    >
    > I am trying to glob a directory, storing the filenames into an array.
    > The trick is that I then want to sort that array by the files'
    > timestamps. Is there an easy way to do this?
    >
    > TIA!


    For example (UNTESTED):

    my $dir = 'something';

    opendir my $DH, $dir or die "Cannot opendir '$dir' $!";

    my @sorted_files =
    map $_->[1],
    sort { $a->[0] <=> $b->[0] }
    map -f "$dir/$_" ? [ ( stat _ )[9], $_ ] : (),
    readdir $DH;



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Feb 22, 2008
    #4
  5. Guest

    Thanks to all of you, John your code was especially helpful.
     
    , Feb 25, 2008
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Georgy Pruss
    Replies:
    15
    Views:
    947
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    514
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    556
    David M. Cooke
    May 4, 2004
  4. Elbert Lev

    glob.glob unicode bug or feature

    Elbert Lev, Jul 31, 2004, in forum: Python
    Replies:
    5
    Views:
    606
    Neil Hodgson
    Aug 2, 2004
  5. Hitesh

    glob.glob output

    Hitesh, Mar 12, 2007, in forum: Python
    Replies:
    6
    Views:
    636
    Hitesh
    Mar 13, 2007
  6. billiejoex

    glob.glob standardization

    billiejoex, Jun 27, 2007, in forum: Python
    Replies:
    3
    Views:
    401
    Tim Roberts
    Jun 29, 2007
  7. lameck kassana
    Replies:
    0
    Views:
    395
    lameck kassana
    Feb 26, 2009
  8. Replies:
    0
    Views:
    590
Loading...