【问题标题】:date creation with Image::Exiftools or Date::Handler使用 Image::Exiftools 或 Date::Handler 创建日期
【发布时间】:2021-03-02 07:12:23
【问题描述】:

我正在尝试将包含具有不同时区的图片的所有目录合并到一个目录中,统一文件的名称具有相同的格式(匈牙利风格 yyyymmdd_hhmmss),一次消除大量重复。

我正在寻找可以读取文件创建日期而不是最后修改或访问日期的模块(通过仅返回纪元 (stat(file))[9] 的 stat 语句。 我发现 Image::ExifTools 似乎是最有效且最容易列出的。

我注意到,在几次循环进入目录后,模块停止提取正确的日期/时间输出,卡在下一个文件的单调相同的错误日期/时间值中。以下是让我陷入困境的一张图片的列表和下面:更准确地说,win10 上的文件属性给出的创建日期为 20140626 12:16 pm;而是运行我获得的例程 20021208_120000。

foreach $img(@img){
    next if -l $img;
    $img =~ /.+(\..+$)/;
    $ext = $1;
#   %ENV;
    
    $exif=new Image::ExifTool;
    $exif->ExtractInfo($dir.$img);
    $for = $exif->GetValue('CreateDate');
    $for =~ s/$space/\_/g;
    $for =~ s/\://g;
    $for = '_'.$for; 
    
    $size = (stat($dir.$img))[7];
    
#   $date = Date->new($date[9]);
#   @data = $date->array;

#tie my %date, 'Date::Tie', utc_epoch => $date{$date[9]}; #tz => $date{tz};
#my $date = Date::Tie->new( epoch => $date[9] );
%date;  

#   $for = IMG.$for.$ext;
    if (!$all{'IMG'.$for.$ext}){
        $all{'IMG'.$for.$ext}= $size ;
        rename $dir.$img, $dir.'IMG'.$for.$ext;
        print "rename $dir.$img, $dir.'IMG'.$for.$ext\n";
    }elsif($all{'IMG'.$for.$ext} == $size){
        unlink $dir.$img;
        print "Deleting $dir.$img\n";
}

检查所涉及的文件属性,“错误”的似乎具有相同的属性“工作”的:工作和错误状态的属性:创建/获取日期、修改日期和最后访问日期..

我不明白模块故障在哪里。

你有什么推荐吗?有什么不同的模块可以使用吗?

谢谢 西蒙

【问题讨论】:

  • 能否提供一个完整的可运行示例,更多信息见minimal reproducible example
  • “我注意到,在几次循环进入目录后,模块会停止提取正确的日期/时间输出” 您可以将有问题的文件上传到某个地方吗?然后我们可以尝试重现该行为
  • 我无法从您包含的图像中获取创建日期。可以试试this 测试脚本吗?
  • 在 linux 上,我也无法更新 FileCreateDateHere 是我的测试脚本,它给出错误:"This tag is Windows/Mac only in File:FileCreateDate (ValueConvInv)"你是在 Windows 还是 Mac 上?
  • 谢谢 Hakon,我猜我在 win 10 OS、PerlBuilder 2 和 Activeperl 5.xx 工作。我试图复制和粘贴你的 2 个脚本,但是你提到的 2 jpg 代表什么,特别是 1 和 2?如果这两个是我的 jpg,我可以告诉你第一个脚本返回的结果与我的相同。您的第二个脚本跳过语句 if (exists $info->{Error}) { die "Could not extract meta information from '$fn': $info->{Error}"; } 说“警告:$info->{Warning}”如果存在 $info->{Warning};没有任何警报或错误

标签: date perl


【解决方案1】:

在Windows上可以使用Win32API::File::Time读取和修改文件创建时间:

use feature qw(say);
use strict;
use warnings;
use Win32API::File::Time qw(GetFileTime SetFileTime);
use File::Temp qw(tempdir);

my $dir = tempdir( CLEANUP => 1 );
my $fn = 'test.txt';
open (my $fh, '>', $fn) or die "Could not open file '$fn': $!";
say $fh "Foobar";
close $fh;
print_file_times($fn);
my ($atime, $mtime, $ctime) = GetFileTime ($fn);
SetFileTime ($fn, undef, undef, $ctime-180);
print_file_times($fn);

sub print_file_times {
    my ($fn) = @_;
    my ($atime, $mtime, $ctime) = GetFileTime ($fn);
    say "File: $fn:";
    say "  File access time: $atime";
    say "  File modification time: $mtime";
    say "  File creation time: $ctime";
    return $ctime;
}

输出

File: test.txt:
  File access time: 1614640101
  File modification time: 1614640101
  File creation time: 1614639958
File: test.txt:
  File access time: 1614640101
  File modification time: 1614640101
  File creation time: 1614639778

【讨论】:

  • 谢谢我会检查
  • 不幸的是它的工作正常,但 $ctime 不是图片的真正获取日期,而是文件传输到电脑的日期......
猜你喜欢
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
  • 2015-11-24
相关资源
最近更新 更多