DateTime format and spacing

Started by msandersen, November 06, 2022, 11:50:19 PM

Previous topic - Next topic

msandersen

I'm parsing some old image files and adding the date in the Heading and Captions fields.
As an example, consider 2 image files with the dates
Wednesday September 7, 2022 and
Wednesday September 14, 2022
If I run a test on these files
exiftool -d "%A %B %e, %Y" -CreateDate -DateTimeOriginal /filename1 /filename2
you get
======== /Volumes/Photos 8/2022/09-14 test/Image0001 [2022-09-14].jpg
Create Date                     : Wednesday September 14, 2022
Date/Time Original              : Wednesday September 14, 2022
======== /Volumes/Photos 8/2022/09-07 test/Image0001 [2022-09-07].jpg
Create Date                     : Wednesday September  7, 2022
Date/Time Original              : Wednesday September  7, 2022
Note the extra space in single-digit dates. To format this correctly, it needs the date string to be "%A %B%e, %Y"
So, my question is: Is this somehow deliberate, and if it is, do I need to make a custom config-file function to format it properly? All it needs is to strip the extra space in the Day field.

My intended use in a config files is as follows:
# Escape HTML entities
-E
-d
%A %B %e, %Y
# Overwrite the date pattern with DateFmt()
-Headline<$Headline${CreateDate#;DateFmt(", %b %e, %Y")}
-MWG:Description<$MWG:Description&#xa;&#xa;$CreateDate.

It works as intended, except for the problem with the spacing.

StarGeek

On my Windows machine, %e fails completely and it's not shown on the Common Date Format Codes.  But a solution that goes back to one of my very first questions on this site is to use a regex to reformat the result

Using your %e, you could keep the -d (-dateFormat) option the same and add regex to the CreateDate. In this case it would be this which replaces 2 or more spaces with a single space (/(space)(space)+/(space)/g
-Headline<$Headline${CreateDate;s/  +/ /g}
-MWG:Description<$MWG:Description&#xa;&#xa;${CreateDate;s/  +/ /g}


Or the solution using %d would be this which removes leading 0s.  Global flag optional.
-Headline<$Headline${CreateDate;s/\b0//}
-MWG:Description<$MWG:Description&#xa;&#xa;${CreateDate;s/\b0//}


Digging way back, the %e was suggested to me way back then, but didn't work on Windows even then.  Totally forgot about that part.

Exiftool is much more flexible now and the MyDate tag listed there isn't needed anymore, though I still use it to this day.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

msandersen

Thanks StarGeek; I didn't realise you could have inline regex. I know a bit of Regex from years ago with PHP, though my Perl-Fu is weak; I basically know only what I learn from messing with the Config file.
I did a quick Google and ended up on regular-expressions.info
https://www.regular-expressions.info/perl.html#:~:text=Performing%20a%20regex%20search-and-replace%20is%20just%20as%20easy%3A,all%20matches%2C%20and%20not%20just%20the%20first%20one.

I made the following test config file, and the Regex works just great:
# exiftool -@ ~/Exiftool-datetest.args -progress /FILENAME

-IPTCDigest=

-E
-d
%A %B %e, %Y
# Overwrite the date pattern with DateFmt() for the Headline with ", %b %e, %Y"
# Pipe through Regex search-and-replace expression (s///): Replace 2 spaces with 1 => s/  / /
-Headline<$Headline${CreateDate#;DateFmt(", %b %e, %Y");s/  / /}
-MWG:Description<$MWG:description&#xa;&#xa;${CreateDate;s/  / /}.

-overwrite_original
-m

and ran it on some test files; it worked great, the extra space was removed. The other way no doubt works just as well. I left out the g option, as there is only one double space to match.

The heading has a short date format added on a single line, the description adds a long date format with 2 newlines before it.

StarGeek

#3
Quote from: msandersen on November 07, 2022, 02:46:31 AMI didn't realise you could have inline regex.

Yes, you can in-line just about any Perl code, not just RegEx.  See Advanced formatting feature.  Note that this is also leads to Security Issues.

QuoteI did a quick Google and ended up on regular-expressions.info

That is exactly the site I learned regex on a decade ago and post links to it constantly. Yes, this thread is making me feel nostalgic since it got me to thinking about how long I've been using exiftool :D

If you want to learn more about what's happening with s/  / /, you can search on the Perl default variable $_. In the context of exiftool in-lining, the default variable holds the current value of the tag.  The expanded version of your in-lined code would be this, where DateFmt is simply a subroutine in the exiftool code.
$_=DateFmt(", %b %e, %Y");
$_=~s/  / /;
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Phil Harvey

Quote from: StarGeek on November 07, 2022, 10:02:39 AMthis thread is making me feel nostalgic since it got me to thinking about how long I've been using exiftool :D

:) :) :)

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

msandersen

I went back to the OSX man page for strftime, where I got the %e option from, it actually says it puts a blank in front of single digits:
%e    is replaced by the day of month as a decimal number (1-31); single
          digits are preceded by a blank.
As for the Advanced Formatting page, I've come across the page before in search of answers, but I'm task oriented, and don't bother to memorise all the technical documents here. I should bookmark it I suppose, I sometimes have trouble finding some piece of information I know I've seen somewhere on Exiftool, it can be tricky if you don't know what it's called. I sometimes search Google, and find another site which mirrors the Exiftool documentation.
While it is useful to know more about Perl when dealing with Exiftool, I'm not actually trying to study programming. Even so, it helps with config file logic, or even the -if statement logic operators.
I learnt of the DateFmt function only recently when searching the forum for a way of having more than one date format when I came across the post by the guy who wrote it as a Config file function in the first place as a solution to the same issue. Phil was impressed enough to include it as part of Exiftool.

I've got an ever-growing plain text file where I keep Exiftool notes and sample Terminal code for various situations, eg code snippets for formatting any of my manual and vintage lenses, or fixing up my Tamron or Sigma lenses, which doesn't put proper identifying metadata into their files. I keep learning new things I can do, like the -if statement or the -execute to handle several commands in one. It's currently around 700 lines long.
Since I now mainly shoot Fuji, and Photo Mechanic can't write to compressed RAF, I run a stript over the folder to add standard copyright and other address info on the jpgs, then copy everything to the RAF with -tagsfromfile. Pretty niftly.

I even managed to figure out how to use Automator in OSX to make Services so I can right-click a folder and process it from a selection of scripts.
All of which is far more technical than I thought I would get wrangling metadata.

So thanks Phil for your tireless dedication to this amazing tool, I can't think of another program that I use with as vibrant a community and personal help from it's creator.

Phil Harvey

It sounds like you're deep into the metadata jungle, and have become very proficient at certain tasks. :)

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).