Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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:

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


In reply to Re: No. files in folder by tlm
in thread No. files in folder by Win

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found