Simple file rotation script. This script moves files that are sequentially number such as 01.txt, 02.txt, … to a file
of your choosing. It is set to rotate week-n.txt to current in week n of the year. If no file named week-n.txt is found, it
rotates default.txt to current. If neither week-n.txt nor current.txt is found, the script throws an error and exits.
The script is meant to be run once a week via cron. It is rough and not user friendly.
#!/usr/bin/perl
#
#
#
# Rotates numbered files from an archive, to a file in the base directory.
# If the file is not found the default file is rotated.
#
#
# This is a simple script. Do whatever you want with it.
#use strict;
use File::Copy;# Change these to appropriate values as needed.
my $archive_dir = '/path/to/my/newsletter/'; # don't forget the trailing slash
my $prefix = 'site_news'; # newsletter prefix
my $extension = '.php'; # newletter extension, e.g. .txt, .html , .jpg or whatever
my $current = $archive_dir.'current'.$extension; # file rotated to e.g. current.txt
my $logfile = 'log'; # log file
my $default = $archive_dir.'default'.$extension; # default extension# No need to touch anything below here.
my ($mday,$mon,$year,$wday,$daynumber) = (localtime(time))[3,4,5,6,7];
my $timestamp = $year.'-'.$mon.'-'.$mday;
my $weeknumber = int($daynumber/7)+1;
my $monthnumber = sprintf"%02d",$mon+1;
$weeknumber = sprintf"%02d",$weeknumber;my $fname = $archive_dir.$prefix.$monthnumber.$extension;
unless (-e $archive_dir) { die "Archives not found, please create a directory called $archive_dir and upload files to it.\n\n $!"};
if (-e $fname){
copy($fname,$current) || die "Can't copy $fname to $current: $!";
logit($fname,$current);
close(OUT);} else {
copy($default,$current) || die "Can't copy $default to $current: $!";
logit($default,$current);
close(OUT);}
sub logit{
my ($infile, $outfile) = @_;
open(OUT, ">>$logfile") || die "Can't open log: $1";
printf OUT "[%4d-%02d-%02d] ", $year+1900,$mon+1,$mday;
print OUT "$infile copied to $outfile\n";
close(OUT);}