Open a directory and sort files by date created

file, perl, sorting

Solution

You can customize the sorting order by providing a subroutine or code block to the sort function.

- In this sub or block, you need to use the special variables `$a` and `$b`, which represent the values from the @array as they are compared.

- The sub or block needs to return a value less than, equal to, or greater than `0` to indicate whether `$a` is less than, equal to, or greater than `$b` (respectively).

- You may use the special comparison operators (`<=>` 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`.

Problem

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"; } ```

Original source