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»


In reply to How To Modify PDF Metadata [SOLVED] by karlgoethebier

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.