Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Delete Old Directories

by williams554 (Sexton)
on Oct 21, 2011 at 04:47 UTC ( #932817=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^3: Delete Old Directories
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-08-22 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (167 votes), past polls