Delete old report files from PATH for x days in Perl

83 Views Asked by At

I have a script which should create a report on daily basis on some alarm data. These report files(.csv) will be stored in the perticular path.

Before generating the report, I need to look whether there is existance of any old files which are older than x days (passed as an argument to the script based on requirement).

Below is script to delete the old logs/reports.

...
use File::Find;

my $days = $ARGV[0]; #pass as a parameter, i.e., number of days

my $PATH = "/path/to/the/logfiles/";
print "backup path -> $PATH\n";

my @file_list;
my @find_dirs       = ($PATH);           # directories to search
my $now             = time();            # get current time
my $seconds_per_day = 60*60*24;          # seconds in a day
my $AGE             = $days*$seconds_per_day;   # age in seconds
find ( sub {
    my $file = $File::Find::name;
    if ( -f $file ) {
    push (@file_list, $file)
    }
}, @find_dirs);


for my $file (@file_list) {
    my @stats = stat($file);
    if ($now-$stats[9] > $AGE) {
        unlink $file;
    }
}
print "Deleted files older than $days days.\n";

#
# GENERATE REPORT CODE
#
...
...

This script works fine for me. I want to know whether we have any Standard Perl Module which will do the same operation which can act as logrotation.

0

There are 0 best solutions below