#! /usr/bin/perl -w my $TESTING=1; # if set to empty sting, will use script directory/../data my $CONTROL_FOLDER = ''; my $CLEANUP_FILE = 'mail_cleanup.list'; my $CONFIG_FILE_NAME = 'mailmanage.cfg'; my $TEMP_CLEANUP_TIME = 7; sub getScriptLocation { use strict; use File::Spec::Functions qw(rel2abs); use File::Basename; return dirname(rel2abs($0)); } sub readConfig { my $scriptLocation = &getScriptLocation(); my $return = ''; if ( -e "$scriptLocation/$CONFIG_FILE_NAME" ) { open CFG,"<$scriptLocation/$CONFIG_FILE_NAME" or die "Could not read $scriptLocation/$CONFIG_FILE_NAME: $!\n"; $return = join ( '', ); close CFG; } return $return; } sub getFoldersToProcess { my $folderListFile = shift; my @folders = (); open DATA, "<$folderListFile" or die "Could not read $folderListFile: $!\n"; @folders = grep { ! /^#/ } ; close DATA; chomp @folders; return @folders; } sub cleanFolder { my $folderToProcess = shift; my @list; if ( $TESTING ) { @list = `find $folderToProcess -type f -mtime +$TEMP_CLEANUP_TIME -print`; } else { @list = `find $folderToProcess -type f -mtime +$TEMP_CLEANUP_TIME -print -exec rm \\{\\} \\;`; } return scalar( @list ); } $config = &readConfig(); eval( $config ); $CONTROL_FOLDER = &getScriptLocation() . '/../data' unless $CONTROL_FOLDER; $CLEANUP_FILE = "$CONTROL_FOLDER/$CLEANUP_FILE"; my @foldersToProcess = &getFoldersToProcess( $CLEANUP_FILE ); my $totalDeleted = 0; print "TEST MODE; no files deleted\n" if $TESTING; print "Cleaning files older than $TEMP_CLEANUP_TIME Days\n"; foreach my $folder ( @foldersToProcess ) { next unless $folder; my $deleted = &cleanFolder( $folder ); print "$folder\t$deleted\n"; $totalDeleted += $deleted; } print "Deleted $totalDeleted files\n"; 1;