Solved

Sorting readdir results by date

Posted on 2002-05-16
4
1,607 Views
Last Modified: 2010-08-05
Hi,

I'm reading a number of daily log files and displaying their contents. The log entries in the files are ordered by date no problem. However I would like the files to be ordered on my page so that the most recent appears at the top. When reading the filesnames from the directory using readdir is there some way of sorting the results by the last modified date on the file?

This is what I have so far:

# Open directory handle for log directory
opendir(DIR, "log") || print "open dir error: $!";

# Read the log files in the directory into array
@files = grep( /\.log$/i , readdir( DIR) );

foreach $file(@files)
{
  # Print log file name
  print "<h3>$file</h3>\n";

  # Open log file
  open (IN1,"log/$file");

  # Write log file contents
  while (<IN1>)
  {
    print "$_<br>\n";
  }

  # Close log file
  close IN1;

}
0
Comment
Question by:AndyReed
  • 2
4 Comments
 
LVL 4

Expert Comment

by:dda
ID: 7015619
I can suggest to use stat() for getting the date and hash for storing filenames and last modified date. You can't do it with readdir() only.
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 7015675
@files =
    map{(split/ /,$_,2)[1]}
    sort
    map{sprintf"%8x %s",~(stat)[9],$_}
    grep{/\.log$/i}
    readdir DIR;
0
 
LVL 4

Expert Comment

by:dda
ID: 7015697
wow!
0
 

Author Comment

by:AndyReed
ID: 7015905
~(stat)[9] doesn't appear to be working. It returns 0 for each file. I'm afraid I don't understand the ~ Does it call stat with the last value of $_ ? If so then I don't think its working. Do you need 'use' something use this form?

Almost there, your solution works if I use
   map{sprintf"%8x %s",(stat(_$))[9],$_}
but obviously I don't get the filename in the second field.

Many Thanks,
Andy.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now