in reply to Appending date to filename for Archiving

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

  • Comment on Re: Appending date to filename for Archiving

Replies are listed 'Best First'.
Re^2: Appending date to filename for Archiving
by viperl (Initiate) on Jun 30, 2009 at 10:11 UTC
    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,