Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
because if it create directories like: 20111020 20110901 20110920, it will not delete the 20111001. Is there a better way to accomplish this? Here is the code I am talking about:if($file <= $exp_dir_date){...
Thanks for looking!!/usr/bin/perl -w use strict; use POSIX qw(strftime); use Date::Calc qw( Today Add_Delta_Days); # get todays date::: my $file_date = sprintf "%04d%02d%02d", Today(); my $current_dir = "alldir"; my $exp_dir_date = sprintf "%04d%02d%02d",Add_Delta_Days( Today(), -45 + ); my $dir_name = strftime("%Y%m%d",localtime(time)); # when this runs, it will create a new dir in alldir directory. unless(-e $current_dir."/".$dir_name or mkdir ($current_dir."/".$dir_n +ame, 0755)) { die "Unable to create $current_dir."/".$dir_name\n"; } print "\n**\n"; #now open $current_dir and read its content deleting any directory old +er than 45 days opendir (DIR, $current_dir) or die "Couldn't open directory, $!"; while (my $file = readdir DIR) { next if $file=~/^\./; if($file <= $exp_dir_date){ print "\nDELETE:::$file - $file_date - $exp_dir_date\n\n"; rmdir("alldir/$file"); #-------> remove directory }else{ print "\nOK:::$file* = $file_date - $exp_dir_date\n\n"; } } closedir DIR; print "\n\ndone\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Compare dates help!
by Ratazong (Monsignor) on Oct 21, 2011 at 06:35 UTC | |
|
Re: Compare dates help!
by williams554 (Sexton) on Oct 21, 2011 at 04:58 UTC | |
by Anonymous Monk on Oct 21, 2011 at 10:46 UTC | |
|
Re: Compare dates help!
by ikegami (Patriarch) on Oct 21, 2011 at 02:37 UTC |