儘可能簡單的方式,我想知道是否有人知道如何通過將當前「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