Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Files By Date

Status
Not open for further replies.

davef101

MIS
Mar 3, 2006
34
GB
how do i list files in a directory, sorted by date ?

i have a directory of completed order files, which need to be displayed in order of completed date (i.e when the file is created).

Thanks in advance.
 
Why do you need Perl for this?

Windows:
dir /OD
dir /O-D

Unix:
ls -t
ls -rt

hope this helps
 
i have to list the contents of the files in date order for an admin order check list.
 
Not sure if I understood your needs;
I still think that you don't need Perl.

Which operating system are we taking about?

In any case, I would do this:
1) create a list as shown above
2) display files in the order given by 1)

regards
 
Each .txt (the order) file contains the order, date etc .. so i need a way of taking the date either from the file, or from within the file .. then listing them in order ..

the files are liek this ..

Date|29/09/2006
Order|IW20
Qty|4
etc ...
 
Hi,

you didn't answer this question:
Which operating system are we taking about?

And then, I am not sure about your date:
There is a file creation date, which can be used as i showed above.
And then there is a date within your file.
Are those identical? So you could use either of them...
Otherwise, well, you may have to use some tool like perl (or sed or awk or ..) to extract the date from your file.

And as for html: That's a quite new problem.
And I leave that to others.

regards
 
Operating system is Linux.

yes, the dates are the same, within the file and as part of the file make-up.

Bascially, i have to write a perl script to pick each file out of the directory, then display them in order they were created ... in an HTML table.

Thanks.
 
Ok, I am still not convincend that you need a perl script, when a Linux ls -rtl could do the same...
But have it the way you feel comfortable with.
And I am not familiar with converting to html. But I think this is a common problem, and plenty of help should be available on the web.

regards
 
this should get you started:

Code:
chdir('path/to/directory') or die "Can't change directory: $!";
opendir(DIR,'.') or die "Can't open directory: $!";
my %allfiles = map { $_ , (stat($_))[9]} readdir(DIR);
close(DIR);
print '<table>';
my $output = "";
foreach my $file (sort {$allfiles{$a} <=> $allfiles{$b}} keys %allfiles) {
   $output .= "<tr><td>$file</td><td>" . localtime($allfiles{$file}) . "</td></tr>\n";
}
print $output,'</table>';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top