use File::stat; my ($logfile, $oldlogfile, @logfile); my ($date, $sec, $min, $hour, $mday, $mon, $year); my(@months) = 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); $archivedirin = "F:/data/archiveinbound/"; #Directory on the local machine where inbound data to purge resides. $archivedirout = "F:/data/archiveoutbound/"; #Directory on the local machine where inbound data to purge resides. $logdir = "F:/PerlScripts/Logs/"; #Directory on the local machine where logs will be placed from purge. $daysToPurge = 2; # name logfile with current date ($sec, $min, $hour, $mday, $mon, $year) = localtime(time); $year += 1900; $logfile = sprintf "purgelog-%4d-$months[$mon]-%02d.log", $year,$mday; $errorlog = sprintf "errorlog-%4d-$months[$mon]-%02d.log", $year,$mday; $date = localtime; # open log message files open(LOGF, ">>$logdir$logfile") or die "can't open: $logfile\n"; print LOGF "- - $date files that will be removed from archiveinbound - - \n"; open(ERRLOG,">$logdir$errorlog") or die "can't open: $logfile\n"; print ERRLOG "- - errors from removal of files from archiveinbound on $date\n"; # List inbound archive directory opendir(DIR,$archivedirin)or die "can't opendir: $archivedirin\n"; @filename = readdir(DIR); closedir DIR; foreach $filename(@filename){ chomp $filename; my $filepath = "$archivedirin$filename"; stat($filepath); if(-f $filepath){ print LOGF "$filename is older then $daysToPurge days and will be purged.\n" if -M $filepath > $daysToPurge; unlink "$filepath" or print ERRLOG "can not remove file $filename from $archivedirin \n"; } } # end of purge of inbound archive directory print LOGF "- - end of archiveinbound purge $date - - \n"; print ERRLOG "- - end of archiveinbound purge $date - - \n"; # List outbound archive directory opendir(DIR,$archivedirout)or die "can't opendir: $archivedirout\n"; @filename = readdir(DIR); closedir DIR; print LOGF "- - $date files that will be removed from archiveoutbound - - \n"; print ERRLOG "- - errors from removal of files from archiveoutbound on $date\n"; foreach $filename(@filename){ chomp $filename; my $filepath = "$archivedirout$filename"; if(-f $filepath){ print LOGF "$filename is older then $daysToPurge days and will be purged.\n" if -M $filepath > $daysToPurge; unlink "$filepath" or print ERRLOG "can not remove file $filename from $archivedirout\n"; } }