Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Need help sorting and printing file in a directory

Need help sorting and printing file in a directory

Need help sorting and printing file in a directory

Have a script the reads a directory of uploaded files and prints them out. I need to 1: sort them by date (newest printed first) and then 2: print the date next to the filename.  Can anyone help me? thanks.

script below:
   @allfiles = sort(grep(!/^\.\.?$/,readdir(FILES)));
   foreach$file(@allfiles) {
      print "$file\n";

RE: Need help sorting and printing file in a directory

there are easier ways to do this with simple command line tools (ls), but the perl way isn't terribly difficult, so i'll give it to you.

the modification time of a file can be found with either of the following expressions:
$mtime = -M "filename";
$mtime = (stat("filename"))[9];

the first returns the age in days (with decimal part), the second returns a unix time stamp.  i'd suggest using the second.

then, to sort the filenames, use a swartzian transformation:

@sorted = map ( {$_->[1]}
          sort( {$a->[0] <=> $b->[0]}
          map ( {[(-M $_), $_]} @allfiles) ) );

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

RE: Need help sorting and printing file in a directory

a UNIX command line approach - just to illustrate another option.


@files = `ls -tl *`; # -t orders by last modify time.  See 'man ls' for other options
foreach $file (@files)
my @stuff = split(/\s+/,$file);
printf "%-25s%5s%4s%7s\n", ($stuff[8],$stuff[5],$stuff[6],$stuff[7]);

as always, with Perl, TMTOWTDI,

 keep the rudder amid ship and beware the odd typo

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close