News:

2023-03-15 Major improvements to the new Geolocation feature

Main Menu

Extracting part of a Windows JPG filename and copying into an Exif tag

Started by mpegleg, June 14, 2019, 01:48:36 AM

Previous topic - Next topic

mpegleg

Ok. So here's one more slightly advanced scenario of an example of what I eventually want to do... (It might be a new Forum topic required. I just don't know yet what would be involved). It's so hard to search the forum for "date" threads.

I've had a quick look at the DateFmt options, but I don't think they handle more "friendly" longform month date styles like Jan, January, etc.

The reason firstly, is that I want to serve out and display my images on a TV with more friendly date options for my elderly folks who aren't so happy always reading 1940-06-04, etc.


------------------
Eg.
Let's say that I have a Window's JPEG image with the filename: "A sunny day at the seaside.jpg" with a DateTimeOriginal=1940-06-04 (...and note that I'm not concerned about obtaining and displaying the time).

Is there a way that I could have a comment tag created that would contain: "A sunny day at the seaside (June 4, 1940)"
------------------

Note: Thanks to you all, now that I'm up to speed with manipulating the rest of the filename, all I'm now interested in, is creating the "June 4, 1940" string sequence for any given DateTimeOriginal.


Just wondering if ExiFtool has that capability?

Would it require a config file, Regex, or other scripting to do it? ... or is it out of the scope of what is easily possible here?

ps. if it's not easily doable with ExifTool, that's ok, as I do have a backup way of doing it, whereby I can use Rename Expert to rename a temporary folder of files and then import those filenames into the tags, but it's a bit of a long way around, esp. with 1000's of photos in multiple folders.
OS: Windows 11 Pro

StarGeek

You can use either the -d (dateFormat) option to change the date output globally or the DateFmt helper function on individual tags.  Either way you would use standard strftime notation to format it.  There are one or two of those formats that windows doesn't support, I can't recall which, but the most useful ones still work in Windows.

You basic option would be
-d "%B %d, %Y"
but it does have the caveat that there will be leading zeroes.  Your example would return June 04, 1940.  You can strip those with regex, either inline or globally with -api "Filter=s/\b0+\B//g" 

Examples:
"-Comment<${Filename;s/\[PIX\d{6}\] ?//;s/\.[^.]*$//} (${DateTimeOriginal;DateFmt('%B %d, %Y');s/\b0+\B//g})"
or
-d "%B %d, %Y" "-Comment<${Filename;s/\[PIX\d{6}\] ?//;s/\.[^.]*$//} (${DateTimeOriginal;s/\b0+\B//g})"
or
-d "%B %d, %Y" -api "Filter=s/\b0+\B//g" "-Comment<${Filename;s/\[PIX\d{6}\] ?//;s/\.[^.]*$//} (${DateTimeOriginal})"

Fun fact, this demonstrates the perl motto, TIMTOWTDI (Tim Toady).
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

mpegleg

Wow. Thanks StarGeek. That was quick.

Are there any advantages/disadvantages to using any particular one of those examples? Is any one of them more... "robust"?

Preferably the simplest one.
OS: Windows 11 Pro

mpegleg

The first 2 worked as expected, but the 3rd one didn't do anything, although it did say the file was updated. I'm still checking the syntax... might be my fault....

Edit. Ah yes, it was my fault. I was using the wrong tag. I was testing with XPComment rather than comment. *slap face*

They all worked. So does any one have an advantage over the others? I guess I'll create some temporary folders and let them all rip, on a few thousand photos. We shall see :)

The Zen of Python: There should be one-- and preferably only one --obvious way to do it. :P
...but to be honest, I'm so glad there isn't!
OS: Windows 11 Pro

Hayo Baan

I would prefer the first one since that allows you to combine it more easily with (setting) other tags; the second one fixes the output of all dates while the third would apply the "filter" to all tags.
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

OK. Good, because that's the one I've currently got doing the hard yards, and it's working like a charm, so I guess I'll stick with that.

As my father used to say... "If it ain't broke... don't fix it".
OS: Windows 11 Pro

mpegleg

I'm still taking baby steps with this.

My next scenario to speed up the renaming of some of my photos is to use part of the current directory name. So it combines what we have done previously, but now using part of the current directory name.

This is the Directory naming structure I have at present...

Eg.
I have many Windows folders similar to this. For this example, it contains 3 files as follows:
   (Not real photos. But I have created them to test) :o

W:/Photos/John/2018/2018-12-25 Santa's Misfortune/

     2018-12-25 02-35-01 [PIX854974] This is a photo of Santa coming down the chimney.jpg

     2018-12-25 02-36-42 [PIX344563] This is a photo of Woofy biting Santa's leg.jpg

     2018-12-25 03-14-22 [PIX102673] This is the next photo of Santa being treated in hospital.jpg


I would like the the 3 photo's command tags to contain :

Santa's Misfortune - This is a photo of Santa coming down the chimney (Dec 25, 2018)

Santa's Misfortune - This is a photo of Woofy biting Santa's leg (Dec 25, 2018)

Santa's Misfortune - This is the next photo of Santa being treated in hospital (Dec 25, 2018)



Who's up to the challenge? Note the shortened form of the MONTH required. The date should be extracted from DateTimeOriginal.

Thanks in advance to all of you for your time & patience. I'm learning heaps from these practical exercises, and I'm sure it will be very useful to other ETn00bs. Working through all the detailed documentation is taxing, yet these practical examples are so much easier for me to get my head around.  :)
OS: Windows 11 Pro

Hayo Baan

This should work on Mac/Linux:
exiftool '-comment<${directory; use Cwd "abs_path"; $_ = abs_path($_); $_ =~ s/.*\///;} - ${filename; $_ =~ s/.*\] |\....$//g;} (${datetimeoriginal; DateFmt("%b %e, %Y");})' FILES
On Windows, use this (i.e. swap single and double quotes):
exiftool "-comment<${directory; use Cwd 'abs_path'; $_ = abs_path($_); $_ =~ s/.*\///;} - ${filename; $_ =~ s/.*\] |\....$//g;} (${datetimeoriginal; DateFmt('%b %e, %Y');})" FILES
Note: if the directory name doesn't come out right and contains the full path, replace $_ =~ s/.*\///; by $_ =~ s/.*\\//;

In above commands I use abs_path to allow use in e.g. the current directory, %b instead of %B for the abbreviated month name and %e instead of %d to get the day without leading 0.

The result on two of your files:
$ exiftool -comment *.jpg
======== 2018-12-25 02-35-01 [PIX854974] This is a photo of Santa coming down the chimney.jpg
[File]          Comment                         : 2018-12-25 Santa's Misfortune - This is a photo of Santa coming down the chimney (Dec 25, 2018)
======== 2018-12-25 02-36-42 [PIX344563] This is a photo of Woofy biting Santa's leg.jpg
[File]          Comment                         : 2018-12-25 Santa's Misfortune - This is a photo of Woofy biting Santa's leg (Dec 25, 2018)
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

Hayo Baan.

My result on Windows 10 using ExifMixer GUI was:

2018-12-25 Santa's Misfortune - This is the next photo of Santa being treated in hospital ()


I'd rather not have the leading date, and of course... the ending (), has no date??



...and using the CMD command window with a batch file:

2018-12-25 Santa's Misfortune - This is the next photo of Santa being treated in hospital (e, Y)


* I think it might have something to do with the length of the whole string... when combined with the long filepath and filename? EDIT: No. I had a truncation of the command only when I tried dragging and dropping onto a batch file.
OS: Windows 11 Pro

Hayo Baan

Right, overlooked the fact you didn't want the date in the directory name included in the comment, that should be easily fixed:

exiftool "-comment<${directory; use Cwd 'abs_path'; $_ = abs_path($_); $_ =~ s/.*\///; $_ =~ s/^[0-9- ]*//;} - ${filename; $_ =~ s/.*\] |\....$//g;} (${datetimeoriginal; DateFmt('%b %e, %Y');})" FILES

As to why there's no date showing, that's really odd since a similar DateFmt worked for you before... Do the files in fact have a datetimeoriginal and did you really use the Windows version of the command including the double quotes surrounding the whole argument?

The fact that the batch file didn't show anything has to do with Windows interpreting %x as a variable, not a literal. BUT the quotes should have prevented that from happening.
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

Yes. I just tried the same files with StarGeek's example previously and the dates worked OK.

You have fixed the initial date showing, so we're almost there.

Santa's Misfortune - This is the next photo of Santa being treated in hospital (e, Y)
OS: Windows 11 Pro

Phil Harvey

I think that Windows interpolates "%" inside quotes.  So you must double all "%" characters in a Windows .bat file if you are running the command from there.

- 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 ($).

mpegleg

Ok. I tried double quotes in the batch file...

Santa's Misfortune - This is the next photo of Santa being treated in hospital ()
OS: Windows 11 Pro

mpegleg

OK. I did it! :)

I combined the first part of Hayo Baan's with (a very slightly modified version) of the last part of StarGeek's, and it worked! (I also obviously had to change the "B" to "b" to use the Month shortform).

So that means... Hayo+Geek=HayoGeek SUCCESS!!

exiftool "-comment<${directory; use Cwd 'abs_path'; $_ = abs_path($_); $_ =~ s/.*\///; $_ =~ s/^[0-9- ]*//;} - ${filename; $_ =~ s/.*\] |\....$//g;} (${DateTimeOriginal;DateFmt('%%b %%d, %%Y');s/\b0+\B//g})" FILE

comment tag now contains:

Santa's Misfortune - This is the next photo of Santa being treated in hospital (Dec 25, 2018)

Thank you all :) ps. Santa has recovered well.



----------------------------
EDIT: After a bit of reflection and tinkering with the code, I decided to go with this:

"-comment<${Filename;s/\[PIX\d{6}\] ?//;s/^.{29}//;s/\.[^.]*$//} [${directory;use Cwd 'abs_path';$_=abs_path($_);$_=~s/.*\///;$_=~s/^[0-9- ]*//;} - ${DateTimeOriginal;DateFmt('%b %d, %Y');s/\b0+\B//g}]" FILE

(Of course I need to remember to double up on the % signs, when used in a Windows batch file!)


So now,

W:\John\2018\2018-12-25 Santa's Misfortune\

2018-12-25 02-54-58 IMG_0125 [PIX343107] Santa falling down the chimney.JPG

2018-12-25 18-55-36 IMG_0126 After a long day at work, [PIX598541] Santa falls asleep.JPG

2018-12-25 19-02-22 IMG_0127 [PIX779251].JPG


will provide me with 3 comment tags containing:

Santa falling down the chimney [Santa's Misfortune - Dec 25, 2018]

After a long day at work, Santa falls asleep [Santa's Misfortune - Dec 25, 2018]

[Santa's Misfortune - Dec 25, 2018]


which better suits me, as I realize that most of my photos don't have any "friendly" text description (as in my 3rd JPG example), so the square brackets will alert me to that fact that the text I am seeing is a "Collection" Title, rather than a specific description of the photo. Perfect!
OS: Windows 11 Pro

Hayo Baan

Excellent, so as it turns out, you only had to replace my %e with %d and then remove the leading zero in another regexp. I guess the Windows version of strftime (which is called underneath all this) doesn't have %e. You live and learn ;)
Hayo Baan – Photography
Web: www.hayobaan.nl