|No such thing as a small change|
Re: No. files in folderby tlm (Prior)
|on Jun 23, 2005 at 10:47 UTC||Need Help??|
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:
Though, admittedly, the "Schwartzian Transform" bit is probably overkill. The original approach you had:
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