Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: No. files in folder

by tlm (Prior)
on Jun 23, 2005 at 10:47 UTC ( #469355=note: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re^2: No. files in folder
by ank (Scribe) on Jun 23, 2005 at 16:13 UTC

    The Schwartzian Transform is obviously a requirement for this - what if a file is deleted and re-created in the middle of the sort? That would translate into inconsistent results - and you also make a lot less of OS calls.

    Of course, this doesn't address the problem of having the bunch of files changed after you've -M'ed them.

    Since this seems to be a live directory, you should be especially careful of what you are dropping there or you might shoot yourself in the foot.

    -- ank

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://469355]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-04-18 09:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found