windows - 如何在 Windows XP 上使用 Perl 归档 .log 文件?

标签 windows perl logging archiving

以尽可能简单的方式,我想知道是否有人知道如何通过简单地将当前“localtime()”命名为文件名的一部分来将 .log 文件归档到 Windows XP 目录中? (不要假设日志文件上有锁。)我尝试了各种不同的方法来解决这个问题,但无法解决它...而且网络上也没有很好的例子。

这是我要找的:

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: $!";
}

关于windows - 如何在 Windows XP 上使用 Perl 归档 .log 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5759412/

相关文章:

python - 有没有比 PIL.ImageGrab.grab() 更好的截屏方式?

windows - 在 Windows : How to do it correctly? 上安装 CasperJS

iphone - 在 Windows 上模拟 iPad、iPhone

regex - Perl Regex 查找并返回每个可能的匹配项

perl - 是否已经存在修改后的命令调用工具 – 根据负载 – 动态调节作业池?

bash - 具有文件描述符的重复 IO

scala - 如何更改spark中的日志级别?

windows - 我应该在 64 位版本中同时定义 _WIN32 和 _WIN64 吗?

perl - "CODE"对变量意味着什么?

java - Log4j。将日志放入 mongodb