http://qs321.pair.com?node_id=469355


in reply to No. files in folder

The first line opens the folder (aka directory) for reading (though the syntax confuses perl v5.8.4; I think it's a bug in perl). The second line reads in all the files in the folder (including subfolders and, at least in Unix, the '.' and '..' "subfolders"!) and attempts to sort them decreasingly according to how long since each was last modified (as given by the -M operator). (Note that as written this works only if $dir is the current directory, because readdir gives only the filenames, not full paths). The last line attempts to delete (aka unlink) the oldest two files (or subfolders, etc.) if the total number of files and subfolders in the original folder exceeds 20. (I don't understand the purpose of the unary - operator before the 0.)

I guess this is how I would improve on that code:

opendir my $d, $dir or die "Can't readdir $dir: $!"; my @files = grep -f $_, # collect only the files in folde +r map "$dir/$_", readdir $d; # make full pathnames to the file +s closedir $d; # close directory handle # sort using a "Schwartzian Transform" my @sorted = map $_->[ 0 ], # untag sort { $b->[ 1 ] <=> $a->[ 1 ] } # sort according to age map [ $_, -M $_ ], # tag files with their "ages" @files; unlink @sorted[ 0, 1 ] if @sorted > 20;
Though, admittedly, the "Schwartzian Transform" bit is probably overkill. The original approach you had:
my @sorted = sort { -M $b <=> -M $a } @files;
is probably adequate for most situations.

Also, note that because of ties, in some cases there may not be a unique answer to the question "which are the two oldest files". The procedure above only deletes the first two files in an array of files (or rather filenames) sorted according to age since last modification.

Update: Fixed typo: s/Sh/Sch/. Thanks, frodo72.

the lowliest monk