In as simple a way as possible I am wondering if anyone knows how to archive .log files in a Windows XP directory by simply naming them with the current "localtime()" as part of the file name? (Do not assume there is a lock on the log file.) I tried all kinds of different ways of doing this but couldn't solve it... and there are no good examples on the web.
Here is what I am looking for:
for (all files > that 1 day old)
rename file to file.[datestamp].log
end
Well, this looks so easy, I'm probably misunderstanding something. The task is to move for example "yada.log" to "yada.2011-05-04.log"? Then how about this:
use strict;
use warnings;
use File::Copy;
use POSIX qw(strftime);
my $dir = $ARGV[0] or die "Usage: $0 <directory>";
my $now_string = strftime "%Y-%m-%d_%H%M%S", localtime;
opendir DIR, $dir or die $!;
my @files = readdir DIR;
chdir $dir or die $!;
for my $file (@files) {
next if (-d $file);
next unless ($file =~ /^(.*)(\.log)$/i);
my $dst = $1 . "." . $now_string . $2;
move ($file, $dst) or die "Failed to move $file: $!";
}