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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
opendir my $dir, $dir;

This line opens a handle (which is saved as $d) to the directory found at path $dir (so $dir could be defined as '/home/myuser/thedir').

my @f = sort { -M $b <=> -M $a } readdir $d;

This is the sort of line that shows what perl can do. There's a couple of things going on here, so we can break it down to see what's happening. This statement basically works backwards. First the readdir($d) generates a list of files and directories being read from the $d directory handle. If the directory at $dir contains foo1.dat, foo2.dat, subDir1, subDir2, foo3.dat, then you will get those 5 items listed through readdir(). This list of files is then passed to the sort routine, which is the part enclosed in curly braces. The -M $b <=> -M $a sorts the list of files by the last modified time of the file -- the end result from this sort will be the list of files read from the readdir(), which we are placing in the @f array. Because of the way we've sorted the list, the oldest files will be at the front of the array, with the newest files at the end.

unlink @f[0,1] if @f > 20;

As I said earlier, the @f array now contains the list of files, with the oldest files listed at the front of the array (so elements 0 and 1 would be your oldest 2 files). So the conditional statement on the size of @f checks to see if we have more than 20 files, and if so, executes unlink() on the first 2 entries to have them deleted.

I'd probably rewrite the code as follows to also ensure we are only dealing with files, just in case you have any directories in that main directory we do not want to include. Warning: my perl is quite rusty, there is a good chance the following code will not run, and if it does, it may not work as expected.

my $dir = '/path/to/the/files'; opendir(my $d, $dir) or die("opendir() failed: $!"); my @sort_files = sort { -M($b) <=> -M($a) } grep { -f($_) } readdir($d); unlink(map { "$dir/$_" } @f[0, 1]) if (@sort_files > 20);

In reply to Re: No. files in folder by saskaqueer
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 chilling in the Monastery: (7)
As of 2024-04-25 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found