1

できるだけ簡単な方法で、ファイル名の一部として現在の「localtime()」を使用して.logファイルをWindows XPディレクトリにアーカイブする方法を誰かが知っているかどうか疑問に思っていますか? (ログ ファイルにロックがあると想定しないでください。 ) これを行うためにさまざまな方法を試しましたが、解決できませんでした... Web 上には良い例がありません。

これが私が探しているものです:

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

4 に答える 4

2

うーん、これはとても簡単に見えますが、おそらく何かを誤解しています。タスクは、たとえば「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: $!";
}
于 2011-05-04T00:01:29.157 に答える
0

Logfile::Rotateたとえば、これを実行できるモジュールがいくつかあるかもしれません。

于 2011-05-07T14:12:53.147 に答える
0
opendir(FILES, '*logdir*');
my @files = readdir FILES;
closedir(FILES);
foreach(@files){
    system "move $_ $_.".localtime().".log" if -A $_ > *age*;
}
于 2011-05-04T12:36:10.843 に答える
0

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";
于 2011-05-04T17:11:46.197 に答える