perlを使用して、ファイルが存在するフォルダと作成された時刻に基づいてファイルの名前を変更しようとしています。ファイルGOPR1521.MP4とGOPR7754.MP4は、同じ時刻と日付の2つの異なるカメラで作成されたので、その名前を示すことができます。たとえば、12:32:38に作成された.../GoProTravisL/GOPR1521.mp4は123238L_GOPR1520.mp4になり、GOPR7754.MP4は123239R_GOPR7754.MP4になります。今の唯一の問題はタイムスタンプです。私はそれが間違ったタイムゾーンや時間のオフセットに問題があると思うだろうが、分もオフです。 perlに何かありますか?タイムスタンプを取得すると、私は行方不明になっていますか?以下はperlコード、各ファイルの時間に出力するもの、およびOS X上のFinderが作成時間を示すものです。Perlによるファイルの作成時間が間違っています
コード:各ファイルのタイムスタンプの
#!/usr/bin/perl
use Time::Piece;
use File::stat;
use File::Find;
use File::Basename;
use File::Spec;
@files = <$ARGV[0]/>;
find({ wanted => \&process_file, no_chdir => 1 }, @files);
sub process_file {
my($filename, $dirs, $suffix) = fileparse($_,qr/\.[^.]*/);
if ((-f $_) && ($filename ne "")) {
#print "\n\nThis is a file: $_";
#print "\nFile: $filename";
#print "\nDIR: $dirs";
my(@parsedirs) = File::Spec->splitdir($dirs);
my @strippeddirs;
foreach my $element (@parsedirs) {
push @strippeddirs, $element if defined $element and $element ne '';
}
$pardir = pop(@strippeddirs);
#print "\nParse DIR: ", $pardir;
#print "\nFile creation time: ";
$timestamp = localtime(stat($_)->ctime)->strftime("%H%M%S"); #gives time stamp
print $timestamp;
$newname = $timestamp . substr($pardir,-1) ."_". $filename . $suffix;
print "\nRename: $dirs$filename$suffix to $dirs$newname\n";
#rename ($dirs . $filename . $suffix,$dirs . $newname) || die ("Error in renaming: " . $!);
} else {
print "\n\nThis is not file: $_\n";
}
}
出力:
/Volumes/Scratch/Raw/2016-03-21/GoProTravisL/
File: GOPR1520
File creation time: 05-55-21
File: GOPR1521
File creation time: 05-56-18
File: GOPR1522
File creation time: 05-57-44
File: GOPR1523
File creation time: 05-58-49
File: GP011520
File creation time: 05-59-53
/Volumes/Scratch/Raw/2016-03-21/GoProTravisR
File: GOPR7754
File creation time: 06-02-48
File: GOPR7755
File creation time: 06-04-19
File: GOPR7756
File creation time: 06-06-27
File: GOPR7757
File creation time: 00-06-16
File: GP017754
File creation time: 00-19-30
File: GP027754
File creation time: 00-22-20
実際のファイル時刻を使用してLS:
MacTravis:2016-03-21 travis$ ls -lR /Volumes/Scratch/Raw/2016-03-21
total 0
drwxr-xr-x 8 travis admin 272 Apr 9 21:25 GoProTravisL
drwxr-xr-x 9 travis admin 306 Apr 9 21:25 GoProTravisR
/Volumes/Scratch/Raw/2016-03-21/GoProTravisL:
total 21347376
-rw------- 1 travis admin 4001240088 Mar 21 12:04 GOPR1520.MP4
-rw------- 1 travis admin 1447364149 Mar 21 12:31 GOPR1521.MP4
-rw------- 1 travis admin 2140532053 Mar 21 12:45 GOPR1522.MP4
-rw------- 1 travis admin 1649133454 Mar 21 13:00 GOPR1523.MP4
-rw------- 1 travis admin 1691562945 Mar 21 12:21 GP011520.MP4
/Volumes/Scratch/Raw/2016-03-21/GoProTravisR:
total 31941008
-rw------- 1 travis admin 4001129586 Mar 21 12:04 GOPR7754.MP4
-rw------- 1 travis admin 2166255754 Mar 21 12:31 GOPR7755.MP4
-rw------- 1 travis admin 3202301883 Mar 21 12:45 GOPR7756.MP4
-rw------- 1 travis admin 2466803806 Mar 21 12:08 GOPR7757.MP4
-rw------- 1 travis admin 4001257192 Mar 21 11:27 GP017754.MP4
-rw------- 1 travis admin 516025454 Mar 21 11:29 GP027754.MP4
このような小さな変更はすべての違いをもたらしました。ありがとうございました。 – traisjames