Could someone please help me figure out how to sort files in a directory by date? There are a lot of files to sort, thousands. All I want to do is sort by date last modified or accessed and then print that array.

Thanks,
Derek

Recommended Answers

All 6 Replies

You need to use a file test operator -M or the stat() function to get the date and the sort() function to sort them. But I bet a Google search would find plenty of examples of code already written, so search yourself:

http://www.google.com/search?q=perl+sort+files+by+date

You need to use a file test operator -M or the stat() function to get the date and the sort() function to sort them. But I bet a Google search would find plenty of examples of code already written, so search yourself:

http://www.google.com/search?q=perl+sort+files+by+date

Smart comments are so unecessary. If there were plenty of examples of code already written by searching Google, don't you think I would have used it, instead of posting on here for help? I DID search myself.

I used the -M operator and sorted that way, but it takes a very long time. Is that normal for that many files? I'd like something faster, if possible.

**Real help only, please**

Derek

commented: ungracious +0

The google search found many many examples, so I assume you didn't search, and I assume you don't want anymore help from me. I'll just take my smart ass over to another thread and help people that aren't hyper sensitive.

Member Avatar for onaclov2000

I'm not sure if it's fast or not, but can you do a ls-t, grep the names, then print them?
Ex.
@x = `ls -t`;

foreach $x (@x)
{
print $x;
}
$asdf = <>;


That was pretty quick with like 20 files, I don't know the throughput for 1000's of files

I post the occasional Perl tip and or whatever I can think of that might be helpful to people that I had a hard time figuring out how to do at my blog
Onaclov Nation

I'm not sure if it's fast or not, but can you do a ls-t, grep the names, then print them?
Ex.
@x = `ls -t`;

foreach $x (@x)
{
print $x;
}
$asdf = <>;


That was pretty quick with like 20 files, I don't know the throughput for 1000's of files

I post the occasional Perl tip and or whatever I can think of that might be helpful to people that I had a hard time figuring out how to do at my blog
Onaclov Nation

appreciate the help. I'm using a windows machine...will "ls" work on windows? haven't had a chance to try it yet...but i know ls doesn't work in the command shell

Member Avatar for onaclov2000

appreciate the help. I'm using a windows machine...will "ls" work on windows? haven't had a chance to try it yet...but i know ls doesn't work in the command shell

I ran into that question on another topic I was discussing with a co-worker, we suspect since i have cygwin setup that might be skewing my answer,
ALTHOUGH we should be able to use the dir command,

After checking out MSDN:
MSDN

we find out:
/o [[:]SortOrder]
where:
d = By date and time, earliest first

I'm not at a windows machine at the moment, but it looks like you can replace ls -t with
dir /od

Good luck let me know how it works out,
If you want I have a few posts on Perl and some other various topics on my blog and you're welcome to check it out.
Onaclov Nation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.