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

Simple script reads directory contents and displays them. I need to 1. display them in order of creation/upload date; and if possible, display the date they were created/uploaded. code below (SAVE_DIRECTORY is the set in the beginning of the script):
opendir(FILES,"$SAVE_DIRECTORY"); @allfiles = sort(grep(!/^\.\.?$/,readdir(FILES))); closedir(FILES); foreach$file(@allfiles) { print "$file\n"; } exit; }
thanks for the help, -mo

Replies are listed 'Best First'.
Re: need helping sorting
by DeaconBlues (Monk) on Mar 15, 2001 at 01:58 UTC

    Instead of using opendir/closedir, try using Tie::Dir . The keys of the tied hash are the filenames and the values are the results of the file's `stat`.

Re: need helping sorting
by Hot Pastrami (Monk) on Mar 15, 2001 at 02:15 UTC
    Let's see... this may do the trick (untested):
    opendir(FILES,"$SAVE_DIRECTORY"); @allfiles = sort { (stat $a)[9] <=> (stat $b)[9] } (grep(!/^\.\.?$/,re +addir(FILES))); closedir(FILES); foreach $file (@allfiles) { print "$file " . (stat $file)[9] . "\n"; } exit;
    ...of course this sorts by Last Modified date, so far as I know there isn't a way to sort by actual file creation date.

    Hot Pastrami

      This will work, but it's not so efficient (you end up redoing the stat calls on each file in that sort). So what you might do is grab the files and their modification times, storing that in a hash (filename => modtime) then sort on modtime, e.g. :

      opendir FILES, $dirname or die "Can't open $dirname:$!\n"; chdir $dirname; #to make sure we're in the right directory! # build the hash, in one line! YES! IF YOU ACT NOW, ALL THIS AND MOR +E # CAN BE YOURS! ... </perl_salesman> my %files = map {$_ => -M $_} grep { !/^\.{1,2}$/ } readdir FILES; closedir FILES; foreach my $file ( sort { $files{$a} <=> $files{$b} } keys %files) { printf("file %s was last modified %.2f days ago", $file, $files{$f +ile}); }

      HTH

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      tried it...
      print "Content-type: text/html\n\n"; opendir(FILES,"$SAVE_DIRECTORY"); #@allfiles = sort(grep(!/^\.\.?$/,readdir(FILES))); @allfiles = sort { (stat $a)[9] <=> (stat $b)[9] } (grep(!/^\.\.? +$/,readdir(FILES))); closedir(FILES); foreach$file(@allfiles) { #print "<a href=\"/apply/$file\">$file</a><br>\n"; print "<a href=\"/apply/$file\">$file</a>" . (stat $file)[9] . " +<br>\n"; } exit;
      got this output:
      resume_.1268.1 tm06b.jpg coolurls.jpg r.xls cb-appt.htm test.html982617983 aaa.txt982624016
      which is not sorted by date...also, not sure what the #'s on the last 2 lines are... any more ideas??
        Oops, need to specify the path when using stat():
        opendir(FILES, $SAVE_DIRECTORY); @allfiles = sort { (stat "$SAVE_DIRECTORY/$a")[9] <=> (stat "$SAVE_DIR +ECTORY/$b")[9] } (grep(!/^\.\.?$/,readdir(FILES))); closedir(FILES); foreach $file (@allfiles) { print "$file " . (stat "$SAVE_DIRECTORY/$file")[9] . "\n"; } exit;
        The number at the end is the modify date in Unix epoch format. You may want to translate it to a more readable format, but I can't remember how to do that right now (WAY too much Java this week).

        Update: I think the module to convert the epoch date to a more useable one is Date::Manip, if I recall correctly.

        Hot Pastrami