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

发布于 2024-11-03 02:28:00 字数 290 浏览 4 评论 0原文

以尽可能简单的方式,我想知道是否有人知道如何通过简单地使用当前的“localtime()”作为文件名的一部分来命名它们,从而将 .log 文件存档在 Windows XP 目录中? (不要假设日志文件上有锁。)我尝试了各种不同的方法来执行此操作,但无法解决它......并且网络上没有好的示例。

这是我正在寻找的:

for (all files > that 1 day old)   
  rename file  to  file.[datestamp].log
end

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轻拂→两袖风尘 2024-11-10 02:28:00

嗯,这看起来很简单,我可能误解了一些东西。任务是将例如“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: $!";
}

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: $!";
}
回忆追雨的时光 2024-11-10 02:28:00
opendir(FILES, '*logdir*');
my @files = readdir FILES;
closedir(FILES);
foreach(@files){
    system "move $_ $_.".localtime().".log" if -A $_ > *age*;
}
opendir(FILES, '*logdir*');
my @files = readdir FILES;
closedir(FILES);
foreach(@files){
    system "move $_ $_.".localtime().".log" if -A $_ > *age*;
}
并安 2024-11-10 02:28:00

这就是我最终是如何做到的,感谢 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";

Here is how I ultimately did it, thanks to TLP ! This script is buggy on line #12 but it barely works.

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";
若有似无的小暗淡 2024-11-10 02:28:00

也许有一些模块可以为您执行此操作,例如 Logfile::Rotate例如。

Perhaps there are some modules that can do this for you, somehting like Logfile::Rotate for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文