Ok below is the modified product. Thanks for the suggestions. The only other thing that I would like to add is the ability to change the way the logs are handled. I would love to be able to have this:
** $date_script_was_run ***
File foo has been deleted.
File bar has been deleted.
Dir foobar has been deleted.
** $an_earlier_date_script_was_run ***
File foo has been deleted.
File bar has been deleted.
Dir foobar has been deleted.
Could I just add another sub to handle this and call the sub within the logging statements? So how could I change this to reflect that?
} else {
open(ERRLOG, ">>$errorlog") or warn "scrapping error output\n";
+
eval { die "($!)"; };
if ($@) {
print ERRLOG "Cannot delete dir $_ : $@!\n";
}
}
Anyhow, here is what I have so far. If you can give me a tip on what to look through, or point me in the right direction that would be great.
#!/usr/bin/perl -w
use strict;
use File::stat;
use File::Find qw(finddepth);
my($create_date, $current_date, $time_month, $month_old);
my($dir,$dirs,$logfile,$logfile2,$errorlog);
$_ = *File::Find::name;
$month_old = 2592000;
$current_date = time;
$dir = 'd:\data\Transfer';
$logfile = 'd:\data\log.txt';
$logfile2 = 'd:\data\log_keep.txt';
$errorlog = 'd:\data\errorlog.txt';
finddepth \&deletion, $dir;
#
# Start of my function 'deletion'
#
sub deletion {
if (-f) {
$create_date = stat($_)->ctime;
if ($create_date < ($current_date - $month_old)) {
if (unlink ($_)) {
open(LOG, ">>$logfile") or warn "discarding logfile output
+\n";
print LOG "FIle: $_ - has been deleted.\n";
close (LOG) or warn "Can't close $logfile: $!";
} else {
open(ERRLOG, ">>$errorlog") or warn "scrapping error outpu
+t\n";
eval { die "($!)"; };
if ($@) { print ERRLOG "Cannot delete file $_ : $@!\n"; }
}
} else {
open(LOG, ">>$logfile2") or warn "discarding logfile output\n"
+;
print LOG "The file $_ is newer than 30 days.\n";
close(LOG) or warn "Can't close $logfile2: $!";
}
} else {
$create_date = stat($_)->ctime;
if ($create_date < ($current_date - $month_old)) {
if (rmdir ($_)) {
open(LOG, ">>$logfile") or warn "discarding logfile output
+\n";
print LOG "Directory: $_ - has been deleted.\n";
close (LOG) or warn "Can't close $logfile: $!";
} else {
open(ERRLOG, ">>$errorlog") or warn "scrapping error outpu
+t\n";
eval { die "($!)"; };
if ($@) { print ERRLOG "Cannot delete dir $_ : $@!\n"; }
}
} 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 $logfile2: ($!)";
}
}
}
Thanks,
djw