尽可能简单的方式,我想知道是否有人知道如何通过将当前“localtime()”命名为文件名的一部分来将.log文件存档到Windows XP目录中? (不要假设日志文件上有锁定)我尝试了各种不同的方式来做到这一点,但无法解决它...并没有在网络上的好例子。如何在Windows XP上使用Perl存档.log文件?
以下是我在寻找:
for (all files > that 1 day old)
rename file to file.[datestamp].log
end
尽可能简单的方式,我想知道是否有人知道如何通过将当前“localtime()”命名为文件名的一部分来将.log文件存档到Windows XP目录中? (不要假设日志文件上有锁定)我尝试了各种不同的方式来做到这一点,但无法解决它...并没有在网络上的好例子。如何在Windows XP上使用Perl存档.log文件?
以下是我在寻找:
for (all files > that 1 day old)
rename file to file.[datestamp].log
end
嗯,这个看起来那么容易,我可能误解的东西。任务是将“yada.log”移动到“yada.2011-05-04.log”?那么这个怎么样:
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: $!";
}
opendir(FILES, '*logdir*');
my @files = readdir FILES;
closedir(FILES);
foreach(@files){
system "move $_ $_.".localtime().".log" if -A $_ > *age*;
}
下面是我最终做到了,感谢TLP!这个脚本是#12线上的越野车,但它几乎不起作用。
use strict;
use warnings;
use File::Copy;
use POSIX qw(strftime);
my $dir;
my $file;
if (@ARGV = 2) {
print "Two args accepted.";
$dir = $ARGV[0] or die "Usage: <directory> <filename>";
$file = $ARGV[1] or die "Usage: <directory> <filename>";
goto runblock;
}
else { print "Number of arguments must be 2: directory filename\n"; goto fail; }
runblock: print "Renaming $file .\n";
chdir $dir or die $!;
my $now_string = strftime "%Y-%m-%d_%H%M%S", localtime;
if (-e $file) {
if (-A $file > 7) {
my $dst = "siteAlive." . $now_string . ".log";
move($file, $dst) or die "Failed to move $file: $!";
goto endscript;
}
}
fail: print "Failed to rename $file . Try again.\n" ;
endscript: print "End of script.\n";
也许有一些模块可以为你做这个,比如Logfile::Rotate
。
你确定你有足够的理由来重塑/重新实现[logrotate](http://linux.die.net/man/8/logrotate)吗? – abbot 2011-04-22 19:52:50
为什么不使用'logrotate'?通常在大多数Linux发行版上提供,或者如果使用不提供软件包的nix系统,则可以从源代码编译。 – ewh 2011-04-22 19:53:09
是否有可用于Windows XP的logrotate二进制文件? – djangofan 2011-04-22 20:28:35