Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Delete Old Directories

by Anonymous Monk
on Oct 20, 2011 at 22:32 UTC ( #932772=note: print w/replies, xml ) Need Help??


in reply to Re: Delete Old Directories
in thread Delete Old Directories

I'm trying with something like the code below, but I am not sure on line 31  if($file <= $exp_dir_date){... 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:
#!/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^3: Delete Old Directories
by williams554 (Sexton) on Oct 21, 2011 at 04:47 UTC

    Give this a try

    #!/usr/bin/perl use strict; use POSIX qw(strftime); use Date::Calc qw( Today Add_Delta_Days); my $older_than = 45; # 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 my @files = </$current_dir/*>; foreach my $filename (@files) { next unless (-d $filename); # this is only letting dirs get past if ( int( -M $filename) > $older_than) { print"dir is old:$filename\n";#print what is deleted here. # test what you are going to delete #&erase_directory ($filename); # after test uncomment this #rmdir $filename; # after test uncommnet this } } sub erase_directory { my $directory = shift @_; my @files = <$directory/*>; foreach (@files){ unlink "$_"; } }

    Good luck, Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932772]
help
Chatterbox?
[1nickt]: I had an interview couople of weeks ago, heard from a principal engineer that a friend of his who works at The Google told him that the self-teaching software that determines the dynamic pricing for their adverts...
[1nickt]: ... has become so complex that there is no one at the Goole who knows what it does.
[marto]: William Gibson was right
[moritz]: a classical problem with machine learning
[1nickt]: moritz isn;t the difference today that the machine learning algos are in charge of most stuffs?
[moritz]: depends on what you mean by "most stuff"
[1nickt]: I am sure I don't know about most of the stuff that is no longer controlled by humans. I do know that it's past the point of determining consumer pricing and into things that can kill me with blunt force!

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-11-21 13:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (301 votes). Check out past polls.

    Notices?