0

如何将系统日期附加到 perl 中的文件名?

由于我对 perl 编程非常陌生,你能给我一个上面查询的简单例子吗?

4

2 回答 2

4

POSIX模块中的strftime()函数提供了一种简单的方法来获取您想要的任何格式的日期。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use POSIX 'strftime';

my $date = strftime '%Y-%m-%d', localtime;
say $date;

然后,您可以在文件的文件名中使用该字符串。如果要重命名文件,则可以使用move()File ::Copy模块。

于 2013-08-30T12:28:04.887 回答
2

这可能会帮助你!

#!/usr/bin/perl
my $date=`date +%Y%m%d`;
chomp($date);
my $source_file="/tmp/fileName_.tgz";
my $destination_file="/misc/fileName_" . $date . ".tgz";
print "$source_file\n";
print "$destination_file\n";
system("sudo mv /tmp/fileName_.tgz /misc/fileName_$date.tgz");

或试试这个

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900; 

my $out = "$dir/$host-$mday-$mon-$year"

或者这个

# grab the current time
my @now = localtime();

# rearrange the following to suit your stamping needs.
# it currently generates YYYYMMDDhhmmss
my $timeStamp = sprintf("%04d%02d%02d%02d%02d%02d", 
                        $now[5]+1900, $now[4]+1, $now[3],
                        $now[2],      $now[1],   $now[0]);

# insert stamp into constant portion of file name.
# the constant portion of the name could be included 
# in the sprintf() above.
my $fileName = "File$timeStamp.log";
于 2013-08-30T11:45:36.343 回答