I found this to be an interesting exercise in Date::Pcalc (this is the Perl only version of Date::Calc).

This demo script uses File::Find to go through a given directory and prints out all files older than a given date limit, in business days, that is, without counting Saturdays or Sundays (but still without legal holidays - note that can be done, too, with the Date::Pcalc holiday calendar feature).

Run like this for demo: perl older_than_days.pl -v 10

#!/usr/bin/perl -w use strict; use File::Find; use Date::Pcalc qw(:all); use Getopt::Std; my %opts = (); getopts('d:l:v' , \%opts); die "\nUsage: $0 [-d startdirectory] [-v verbose] age_limit_in_days \n +\n" unless @ARGV; my $start_dir = $opts{d} || "."; my $verbose = $opts{v} || 0; my $day_limit = $ARGV[0] || "10"; find(\&wanted, $start_dir); sub wanted { my $filename = $_; my $age = (stat $filename)[8]; print "Processing file : " . $filename, "\n" if $verbose; return unless is_older_than($age, $day_limit); # put your delete code here - to dangerous to actually do it in de +mo script print "Would delete $filename, it's older than $day_limit business + days ...\n\n"; } sub is_older_than { my ($age, $days) = @_; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me($age); # months start at 1 in Date::Pcalc and at 0 in localtime $mon++; # year is base 1900 in localtime, base 0 in Date::Pcalc $year += 1900; if ($verbose) { print "Today is : " . join ' ', Today(), "\n"; print "File last accessed : " . join ' ', $year, $mon, $mday, "\n"; print "File age in days : " . Delta_Days($year, $mon, $mday, Today()), "\n"; print "File age in business days: " . Delta_Business_Days($year, $mon, $mday, Today()), "\n"; } return 1 if Delta_Business_Days($year, $mon, $mday, Today()) > $da +ys; return 0; } # From the Date::Pcalc manual # 15) How can I calculate the difference in days between dates, but # without counting Saturdays and Sundays? sub Delta_Business_Days { my(@date1) = (@_)[0,1,2]; my(@date2) = (@_)[3,4,5]; my($minus,$result,$dow1,$dow2,$diff,$temp); $minus = 0; $result = Delta_Days(@date1,@date2); if ($result != 0) { if ($result < 0) { $minus = 1; $result = -$result; $dow1 = Day_of_Week(@date2); $dow2 = Day_of_Week(@date1); } else { $dow1 = Day_of_Week(@date1); $dow2 = Day_of_Week(@date2); } $diff = $dow2 - $dow1; $temp = $result; if ($diff != 0) { if ($diff < 0) { $diff += 7; } $temp -= $diff; $dow1 += $diff; if ($dow1 > 6) { $result--; if ($dow1 > 7) { $result--; } } } if ($temp != 0) { $temp /= 7; $result -= ($temp << 1); } } if ($minus) { return -$result; } else { return $result; } } # NOTE THIS: # This solution is probably of little practical value, # however, because it doesn't take legal holidays into # account.

Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com


In reply to Re: find "x" quantity newest files in a dir by clemburg
in thread find "x" quantity newest files in a dir by braintoast

Title:
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, details, 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, summary, 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.