viperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am trying to append filename to a logfile when i archvie it. A day old file needs to be renamed to the previous day. Example. output.log to be renamde to output.log.20090628 I have the following code, that checks if the age is 1 for the file. If it is one day old, then I need to rename it. If it is 14 days old I need to delete it.
chdir $main::logFolder or die "can't cd"; # I am not sure what to populate here for $yesterday $Yesterday; foreach (glob("*")) { $age = -M; if ($age==1) { #rename Log File to .log.yyyymmdd rename ($_, $_ .$Yesterday" } else if($age > 14) { unlink $_; }

Replies are listed 'Best First'.
Re: Appending date to filename for Archiving
by jwkrahn (Abbot) on Jun 29, 2009 at 12:24 UTC
    foreach ( glob '*' ) { my $file_date = POSIX::strftime '%Y%m%d', localtime( ( stat )[ 9 ] + ); my $age = -M _; if ( $age >= 1 && $age < 2 ) { #rename Log File to .log.yyyymmdd rename $_, $_ . $file_date; } elsif ( $age > 14 ) { unlink $_; }
      Thanks for that jwkrahn. Can you explain taht statement. I want to get an understanding of what this sattement does
      my $file_date = POSIX::strftime '%Y%m%d', localtime( ( stat )[ 9 ]+ );

        That creates a string of the year, month, and day of the mtime of the current file name in $_.   Since you are testing the mtime via the -M operator, that date should be the same as yesterdays date, however you could calculate it yourself if that is what you require.

Re: Appending date to filename for Archiving
by CountZero (Bishop) on Jun 29, 2009 at 13:44 UTC
    Will your code run every day,: weekdays, week-end days, holidays, ... included? If not your logic must take the days when the script does not run into account, otherwise you are going to miss some files.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Hi Guys, I tried using your recommendation. I get the following errors.
      Subroutine main::ctime redefined at /usr/lib/perl5/5.8.5/Exporter.pm line 65. at bstpcleanup.pl line 8 Prototype mismatch: sub main::ctime: none vs (;$) at /usr/lib/perl5/5.8.5/Exporter.pm line 65. at bstpcleanup.pl line 8 Unquoted string "age" may clash with future reserved word
      #!/usr/bin/perl -w use POSIX; use POSIX qw(strftime); use File::stat; use Time::localtime; $main::logFolder = "/apps/live/mail/logs"; $main::Folder = "/apps/live/mail/files/Archive"; print ctime()." :Start of File Cleanup\n"; $ClientName = 'abacs'; # Deleting Files that are older than 14 days chdir $main::Folder or die "can't cd"; foreach (glob("*")) { $age = -M; if ($age > 14) { Logit ( "$_ is over 14 days old" ); print "Deleting $_ " ; unlink $_; } else { Logit("$_ is ",int($age)," days old"); } } # Renaming the t-1 day log file and deleting log files older than 14 d +ays chdir $main::logFolder or die "can't cd"; foreach (glob("*")) { my $file_date = POSIX::strftime '%Y%m%d', localtime( ( stat )[ 9 ]+ ) +; $age = -M; if ($age>=1 && age < 2) { #rename Log File to .log.yyyymmdd rename ($_, $_ .$file_date" } else if($age > 14) { unlink $_; } } sub Logit{ my $message = shift(@_); if ($message =~ /^ERROR.*/){ print "ERROR"; } open (LOGF, '>>',$main::logFile); if (-w LOGF){ print LOGF ctime(). " $ClientName $message\n"; } else { print "Cannot open $main::logFile for writing, $!\n"; } close (LOGF); }
      I think I am not using POSIX correctly. Can you help. Thanks,
        Hi All, Can you help me with the above usage of POSIX. Thank you,