Below is a script used for checking a directory and all sub directories for anything that is over 30 days old. If the item is found to be older than 30 days then it gets deleted (this is on a win32 system *choke* using Active Perl).
This is used on a temp directory that I need to clean out every once and a while. The code works but not to my satisfaction. I would like to be able to send all errors to an error.txt file. How do you send output of a 'warn' or 'die' to a file?
I am also having a problem with the program trying to delete the actual 'foo' directory. The last line of my log file says:
'Directory: . - has been deleted.'
And I get an error message on the console that says:
'Cannot delete directory: . (Permission denied) at clear_transfer.pl line 58.'
I tried to use the Cookbooks example of ignoring '.' in a directory match, but then the program ignored all the directories that were over 30 days (obviously because the program was in whatever subdirectory it was trying to delete and wouldn't because that would match '.').
Those are the two things bothering me at the moment. If you see something else, please don't hesitate to point it out.
Thanks,
djw
#!/usr/bin/perl -w
use strict; # uuuuussseeee thiiiissss
use File::stat; # need this for ctime (creation time using sec
+onds since epoch)
use File::Find qw(finddepth); # need this for recursive directory
my($create_date, $current_date, $time_month, $month_old);
my($dir,$dirs,$logfile,$logfile2);
##############################
# good little purl h4x0r's #
# define their variable's #
##############################
$_ = *File::Find::name;
$month_old = 2592000; # set this to seconds. 2592000 = 30 d
+ays
$current_date = time; # gives current time since the epoch
+in seconds
$dir = 'c:\temp\foo';
$logfile = 'c:\temp\log.txt';
$logfile2 = 'd:\temp\log_keep.txt';
########################################
# The switch is flipped! #
# using File:Find:finddepth #
# to step through all subdirectories #
# using my function to test and delete #
########################################
finddepth \&deletion, $dir;
#############################################
# in my sub I start with files then #
# directories. Each creation date is #
# tested before the nested if statements #
# are run so we can see if the data needs #
# deletion or not (oder than 30 days). #
# I also log all deletion and non deletion #
# activity #
#############################################
sub deletion {
if (-f) {
$create_date = stat($_)->ctime;
if ($create_date < ($current_date - $month_old)) {
unlink ($_) or warn "Cannot delete file: $_ ($!)";
open(LOG, ">>$logfile") or warn "discarding logfile output
+\n";
print LOG "FIle: $_ - has been deleted.\n";
close (LOG) or warn "Can't close: $!";
} else {
open(LOG, ">>$logfile2") or warn "discarding logfile outpu
+t\n";
print LOG "The file $_ is newer than 30 days.\n";
close(LOG) or warn "Can't close: $!";
}
} else {
$create_date = stat($_)->ctime;
if ($create_date < ($current_date - $month_old)) {
rmdir ($_) or warn "Cannot delete directory: $_ ($!)";
open(LOG, ">>$logfile") or warn "discarding logfile output
+\n";
print LOG "Directory: $_ - has been deleted.\n";
close (LOG) or warn "Can't close: $!";
} else {
open(LOG, ">>$logfile2") or warn "discarding logfile outpu
+t\n";
print LOG "The dir $_ is newer than 30 days.\n";
close(LOG) or warn "Can't close: $!";
}
}
}