Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Delete directories older than 60 days

by Anonymous Monk
on Nov 14, 2014 at 19:14 UTC ( [id://1107236]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have a list of directories named as
11012014 11132014 10092014 09092014 11112015 01012015 04012015 01042011 10122014
I am trying to delete if they are older than 60 days based on their names, but not getting any luck the way I am trying.
... use File::Path qw( rmtree ); use Date::Simple ('date', 'today'); ... my $dir = '/dir/'; opendir (DIR, $dir) or die "Couldn't open $dir directory, $!"; while (my $directories = readdir DIR) { next if $directories=~/^\./; my $dir_found = $directories; $dir_found =~s/(\d{2})(\d{2})(\d{4})/$3-$1-$2/; # Difference in days between the dates: if (date($dir_found) - date($months_ago); if($days_diff >= 60){ rmtree("$dir/$directories")|| die ("ERROR:::cant delete: $!") +; } } closedir DIR;
Is there a better way of doing this?
Thanks for looking!

Replies are listed 'Best First'.
Re: Delete directories older than 60 days
by toolic (Bishop) on Nov 14, 2014 at 20:13 UTC
    If you're stuck on the date portion, try this:
    use warnings; use strict; use Date::Simple qw(date today); my $today = today(); while (<DATA>) { chomp; if (/(\d{2})(\d{2})(\d{4})/) { my $dir_date = date(sprintf '%4d-%02d-%02d', $3, $1, $2); print "delete $dir_date\n" if ($today - 60) > $dir_date; } } __DATA__ 11012014 11132014 10092014 09092014 11112015 01012015 04012015 01042011 10122014

    Output:

    delete 2014-09-09 delete 2011-01-04
      Yes, it worked:
      ... use File::Path qw( rmtree ); use Date::Simple ('date', 'today'); ... my $dir = '/dir/'; opendir (DIR, $dir) or die "Couldn't open $dir directory, $!"; while (my $directories = readdir DIR) { next if $directories=~/^\./; if ($directories=~/(\d{2})(\d{2})(\d{4})/) { my $dir_date = date(sprintf '%4d-%02d-%02d', $3, $1, $2); if (($today - 60) > $dir_date) { rmtree("$dir/$directories")|| die ("ERROR::: cant delete: $!"); } } } closedir DIR;
      I woder if it could be done with "Time::Piece;" instead of "Date::Simple".
      But thanks for the help!
        Yes. In fact it even simplifies the code, and you can use the Core Time::Piece module:
        use warnings; use strict; use Time::Piece qw(); use Time::Seconds qw(ONE_DAY); my $t = Time::Piece->new(); my $past = $t - (60 * ONE_DAY); while (<DATA>) { chomp; if (/\d{2}\d{2}\d{4}/) { my $dir_date = Time::Piece->strptime($_, '%m%d%Y'); print "delete $_\n" if $past > $dir_date; } } __DATA__ 11012014 11132014 10092014 09092014 11112015 01012015 04012015 01042011 10122014
        #!perl use strict; use Time::Piece; use Time::Seconds; my $today = localtime; while (my $mdy = <DATA>){ chomp($mdy); my $dt = Time::Piece->strptime($mdy,"%m%d%Y"); my $age = int (($today - $dt) / ONE_DAY); print "$mdy $age\n"; } __DATA__ 11012014 11132014 10092014 09092014 11112015 01012015 04012015 01042011 10122014
        poj
Re: Delete directories older than 60 days
by Laurent_R (Canon) on Nov 14, 2014 at 19:43 UTC
    This is probably not going to compile:
    if (date($dir_found) - date($months_ago); if($days_diff >= 60){
    It looks like you changed your mind about on how to do this, but did not make all the changes needed after this change in mind, so that this part of the script hardly make any sense now. Either you create a variable for the difference, or you check the difference directly. Here, your if clause has no conclusion, and $days_diff has not been declared, nor initialized. Similarly, $months_ago has not been declared nor initialized, and the name is suspicious of a possible bug or misunderstanding. I would assume, from your narrative, that you want to compare the directory date against the current date, but that's quite far from what you do.
      my bad, it should read:
      # Difference in days between two dates: my $diff = date('2014-11-14') - date('2014-11-01');
Re: Delete directories older than 60 days
by RonW (Parson) on Nov 14, 2014 at 22:09 UTC

    File::Find can find directories older than a specified time/date.

Re: Delete directories older than 60 days
by Anonymous Monk on Nov 14, 2014 at 19:50 UTC
    There is certainly a better way. Start by telling what exactly is wrong. Error messages etc.
      What is wrong, it will delete all the directories in there if one of them matches the criteria.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1107236]
Approved by Loops
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-24 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found