I need to open directories and sort the files by the time they were created. I can find some discussion, using tags for Perl, sorting, and files, on sorting files based on date of modification. I assume this is a more common need than sorting by date of creation. I use Perl. There is some previous postings on sorting by creation date in other languages other than Perl, such as php or java.
For example, I need to do the following:
opendir(DIR, $ARGV[0]);
my @files = "sort-by-date-created" (readdir(DIR));
closedir(DIR);
do things with @files...
The CPAN has a page on the sort command, but it's not very accessible to me, and I don't find the words "date" or "creation" on the page.
In response to an edit, I should say I use Mac, OS 10.7. I know that in the Finder, there is a sort by creation date option, so there must be some kind of indication for date of creation somehow attached to files in this system.
In response to an answer, here is another version of the script that attempts to sort the files:
#!/usr/bin/perl
use strict; use warnings;
use File::stat; # helps with sorting files by ctime, the inode date that hopefully can serve as creation date
my $usage = "usage: enter name of directory to be scanned for SNP containing lines\n";
die $usage unless @ARGV == 1;
opendir(DIR, $ARGV[0]); #open directory for getting file list
#my @files = (readdir(DIR));
my @file_list = grep ! /^\./, readdir DIR;
closedir(DIR);
print scalar @file_list."\n";
for my $file (sort {
my $a_stat = stat($a);
my $b_stat = stat($b);
$a_stat->mtime <=> $b_stat->mtime;
} @file_list ) {
say "$file";
}
The dir command will sort the files in each directory by the date. It will not sort all the files in date order. To do that you would need to output the file date as YYYYMMDD with the file name and sort the file.
Just click on Date at the top of the column, and then drag it to the left. If you wanted to put the date column last, you'd do the reverse. If you then wanted to sort everything by date, single-click on the date column. Now all the files in the downloads folder will be sorted by date with the oldest items at the top.
Enter any date after datemodified: using the mm/dd/yyyy format to search for any files modified on the desired date. To find files modified between two dates, you can type the following. In this example, any files modified between January 1, 2022, and February 1, 2022, are displayed in the search results.
You can customize the sorting order by providing a subroutine or code block to the sort function.
$a
and $b
, which represent the values from the @array as they are compared. 0
to indicate whether $a
is less than, equal to, or greater than $b
(respectively). <=>
for numbers, cmp
for strings) to do this for you.So the default sort sort @numbers
is equivalent to sort {$a <=> $b} @numbers
.
In the case of sorting by creation time, you can use the stat function to get that information about the file. It returns an array of information about the file, some of which may not be applicable to your platform. Last modification time of the file is generally safe, but creation time is not. The ctime
(11th value that it returns) is as close as you can get (it represents inode change time on *nix, creation time on win32), which is expressed as the number of seconds since the epoch, which is convenient because it means you can do a simple numeric sort.
my @files = sort {(stat $a)[10] <=> (stat $b)[10]} readdir($dh);
I'm not sure if you want to filter out the directories also. If that is the case, you'll probably also want to use grep
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With