karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
i need to set CreateDate and ModifyDate of a many PDFs to the mtime of each particular file.
I must preserve the timestamp of the files.
I tried this:
#!/usr/bin/env perl use strict; use warnings; use POSIX qw (strftime); use Path::Iterator::Rule; use Image::ExifTool; my $dir = q(./pdf); my $tz = strftime( "%z", localtime() ); # my $tz = strftime( "%z", gmtime() ); $tz =~ s/(\d{2})$/':$1'/; my $rule = Path::Iterator::Rule->new; # $rule->file('*.pdf'); # :-( $rule->file->name(qr/.+\.pdf/); $rule->max_depth(1); my $next = $rule->iter($dir); while ( defined( my $file = $next->() ) ) { my $mtime = ( stat($file) )[9]; my $atime = ( stat($file) )[8]; my ( $m_sec, $m_min, $m_hour, $m_day, $m_month, $m_year ) = ( localtime($mtime) )[ 0, 1, 2, 3, 4, 5 ]; # ( gmtime($mtime) )[ 0, 1, 2, 3, 4, 5 ]; $m_year += 1900; my $m_timestamp = sprintf( "%4d:%02d:%02d %02d:%02d:%02d", $m_year, $m_month, $m_day, $m_hour, $m_min, $m_sec ); $m_timestamp .= $tz; my $exifTool = Image::ExifTool->new(); $exifTool->SetNewValue( "ModifyDate", $m_timestamp ); $exifTool->SetNewValue( "CreateDate", $m_timestamp ); $exifTool->WriteInfo($file); utime $atime, $mtime, $file; } __END__
This ends so:
karls-mac-mini:monks karl$ perl -MImage::ExifTool=:Public -E 'say Imag +eInfo("./pdf/a.pdf")->{CreateDate};' 2015:01:07 14:46:51 karls-mac-mini:monks karl$ ls -hl ./pdf/a.pdf -rw-r--r-- 1 karl karl 48K 7 Feb 14:46 ./pdf/a.pdf
I guess this is one month off target.
What do i miss?
Update: Reset state to [UNSOLVED]
Update 2: It seems like this works:
#!/usr/bin/env perl use strict; use warnings; use POSIX qw (strftime); use Path::Iterator::Rule; use Image::ExifTool; my $dir = q(./pdf); my $rule = Path::Iterator::Rule->new; # $rule->file('*.pdf'); # :-( $rule->file->name(qr/.+\.pdf/); $rule->max_depth(1); my $next = $rule->iter($dir); while ( defined( my $file = $next->() ) ) { my $mtime = ( stat($file) )[9]; my $atime = ( stat($file) )[8]; my $m_timestamp = strftime( "%Y:%m:%d %H:%M:%S%z", localtime($mtim +e) ); my $exifTool = Image::ExifTool->new(); + $exifTool->SetNewValue( "ModifyDate", $m_timestamp ); $exifTool->SetNewValue( "CreateDate", $m_timestamp ); $exifTool->WriteInfo($file); utime $atime, $mtime, $file; } __END__
Edit: Fixed wrong rules.
Update 3: Set state to SOLVED again.
Thank you very much for any hint and best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How To Modify PDF Metadata
by hippo (Archbishop) on Jun 13, 2015 at 14:42 UTC | |
by karlgoethebier (Abbot) on Jun 13, 2015 at 16:37 UTC | |
by Laurent_R (Canon) on Jun 13, 2015 at 18:50 UTC | |
by karlgoethebier (Abbot) on Jun 13, 2015 at 21:14 UTC | |
by karlgoethebier (Abbot) on Jun 13, 2015 at 14:57 UTC | |
by Anonymous Monk on Jun 14, 2015 at 06:16 UTC | |
by karlgoethebier (Abbot) on Jun 14, 2015 at 17:52 UTC |