Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: find "x" quantity newest files in a dir

by clemburg (Curate)
on Jun 27, 2001 at 14:24 UTC ( [id://91875]=note: print w/replies, xml ) Need Help??


in reply to find "x" quantity newest files in a dir

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

Replies are listed 'Best First'.
Re (tilly) 2: find "x" quantity newest files in a dir
by tilly (Archbishop) on Jun 27, 2001 at 16:17 UTC
    Actually holidays are not so easy to take into account.

    First of all holidays vary by nation. And by state within nations.

    Secondly holidays change periodically for various reasons. Therefore definitive lists tend not to stay very definitive.

    Thirdly what happens with holidays that land on weekends? Typically different companies will handle these differently. In financial markets there is no hard and fast rule, and the decision about what to do often comes within 12 months of the actual holiday.

    Fourth there are a variety of marginal holidays that are handled differently. A given day may be a holiday in the bond markets but not in the stock market. Or vice versa. Thanksgiving Friday in the US is officially not a holiday, but at many places is. And so on.

    Fifth there is the question of religious holidays. For instance NYU medical school (which has a huge Jewish influence) routinely takes various Jewish holidays off but did not take off Easter. However most places in the US take off Easter but not the Jewish holidays.

    And so on. The closer you look at it, the messier the idea of a "business day" becomes...

      That's why Solaris has /etc/acct/holidays, so that the system administrator can define which days are working days and which aren't for the coming year, for their business.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found