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

时间:2013-08-30 11:39:28

标签: perl date

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

由于我对perl编程很陌生,你能给我一个上述查询的简单例子。

2 个答案:

答案 0 :(得分: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;

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

答案 1 :(得分:1)

这可能会帮助你!

#!/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";