Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2021-04-19 13:29 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found