Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > glob a directory then sort by timestamp

Reply
Thread Tools

glob a directory then sort by timestamp

 
 
jhellma1@gmail.com
Guest
Posts: n/a
 
      02-22-2008
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!
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      02-22-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Tony Curtis
Guest
Posts: n/a
 
      02-22-2008
(E-Mail Removed) 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-22-2008
(E-Mail Removed) 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
 
Reply With Quote
 
jhellma1@gmail.com
Guest
Posts: n/a
 
      02-25-2008
Thanks to all of you, John your code was especially helpful.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i have problem with glob.glob() in remotely directory lameck kassana Python 0 02-26-2009 09:05 AM
glob.glob unicode bug or feature Elbert Lev Python 5 08-02-2004 12:09 AM
Question about glob.glob <--newbie Sean Berry Python 3 05-04-2004 05:34 PM
RE: Bug in glob.glob for files w/o extentions in Windows Tim Peters Python 1 12-01-2003 09:22 AM
Bug in glob.glob for files w/o extentions in Windows Georgy Pruss Python 15 12-01-2003 04:04 AM



Advertisments