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

Oops  :-[ Yes. I must have done something stupid. It works now!

Thanks :)
OS: Windows 11 Pro

mpegleg

Here's a quick & hopefully easy question to answer...

What's the easiest way to output one blank line, or more, (ie. to act as a spacer) between the output for multiple images?

Eg,

I could have:

======== W:/Tests/2019-05-11 12-54-58 IMG_0125 [PIX343107] Santa falling down the chimney modx bla bla bla.JPG
XP Comment                      : Santa falling down the chimney [Tests. 2018]
Comment                         : Santa falling down the chimney [Tests. 2018]
======== W:/Tests/2019-05-11 12-54-58 IMG_0125 [PIX343107] Santa falling down the chimney.JPG
XP Comment                      : Santa falling down the chimney [Tests. 2018]
Comment                         : Santa falling down the chimney [Tests. 2018]
======== W:/Tests/2019-05-11 12-54-58 IMG_0125 [PIX343107].JPG
XP Comment                      : [Tests. 2018]
Comment                         : [Tests. 2018]



look something like this:

======== W:/Tests/2019-05-11 12-54-58 IMG_0125 [PIX343107] Santa falling down the chimney modx bla bla bla.JPG

XP Comment                      : Santa falling down the chimney [Tests. 2018]
Comment                         : Santa falling down the chimney [Tests. 2018]



======== W:/Tests/2019-05-11 12-54-58 IMG_0125 [PIX343107] Santa falling down the chimney.JPG

XP Comment                      : Santa falling down the chimney [Tests. 2018]
Comment                         : Santa falling down the chimney [Tests. 2018]



======== W:/Tests/2019-05-11 12-54-58 IMG_0125 [PIX343107].JPG

XP Comment                      : [Tests. 2018]
Comment                         : [Tests. 2018]
OS: Windows 11 Pro

Phil Harvey

Add -echo "" -echo3 "" -echo3 "" -echo3 "" to your command.

See the -echo option for an explanation.
...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

OS: Windows 11 Pro

Phil Harvey

Ooops. That doesn't do it.  Sorry.  It only puts the lines at the very start and end.

I think you'll need to use the -p option to do what you want.

- 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. Thanks again.

I figured it out (using the -p option with $/ for a newline). It took a bit of experimenting, but it finally worked.  ;)
OS: Windows 11 Pro

mpegleg

I've just realised that there are some nested Windows directories containing "groups" of my photos.

So, I need to be able to add part of the directory name 1 level higher up.

For example, when my folks went on an overseas canal trip to Europe, (because of the large number of pics they took), I stored their photos in a hierarchy of folders, which contains the photos of those days that they were travelling through a certain region.

Here's one cut-down example we can work with:

(Note: I've already stripped out all foreign filename characters, because as expected, that was creating hell with some utilities, and on my Media Players).

W:\Photos\2010-08-08 Jewels of Europe River Cruise\2010-08-13 Rothenburg and Wuerzburg, Germany\

2010-08-12 18-13-10 P1030222 [PIX180903] modx1.jpg
2010-08-13 07-25-22 P1030226 [PIX796859].jpg
2010-08-14 09-51-00 P1030231 [PIX356360] modx2c.jpg
---------------------------------------------------------------------
These filename examples are typical of all those photos in the folders, ie. they will parse as "empty" because there is nothing after the "] " or before modx, so only the directory names will show, but there are some cases where I've added better descriptions to the filenames, (just before the modx), which should get parsed through, as before.

Like in my previous examples, for each photo, I'd like to have the XPComment tag populated with the following:

Rothenburg and Wuerzburg, Germany (Thu. 12 Aug 2010)  [Jewels of Europe River Cruise, 2010]
Rothenburg and Wuerzburg, Germany (Fri. 13 Aug 2010)  [Jewels of Europe River Cruise, 2010]
Rothenburg and Wuerzburg, Germany (Sat. 14 Aug 2010)  [Jewels of Europe River Cruise, 2010]


Note the "Day" ie Thu, Fri, Sat, etc would preferably need to be calculated for each photo.
If that's not possible, don't worry as I can probably do without that, as I'm aware that will "probably" add a lot more complexity(?).

The code to achieve "almost" what I desire (but doesn't yet grab the part of the dir name 1 level higher up, nor day of the week) is:

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


--> This gives: Rothenburg and Wuerzburg, Germany (Aug 12, 2010)

--> but I want: Rothenburg and Wuerzburg, Germany (Thu. Aug 12, 2010)  [Jewels of Europe River Cruise, 2010]

The bold bits added please.

Should be easy peasy for you clever guys. ;) Who's up for the challenge? :)

ps. Anyone that yells config file, config file, will get slapped. lol. Nah... Just kidding.    I'll kick you instead! ;D I'm fairly sure this will be my final coding so, just one line of code please, if possible. If the calculation of the day of the week adds too much complexity, then and only then, I "might" consider using one. :P Let me know what you think.

Cheers,
-mpegleg Paul
OS: Windows 11 Pro

Hayo Baan

Try this:

"-XPcomment<${filename; $_ =~ s/^(?:.*\[PIX\d+\]\s*)?((?:(?!modx).)*)(?:\s*modx.*|\....$)/$1/;s/  +/ /g;s/ +$//;s/^(.+)$/$1 /}${directory; use Cwd 'abs_path';$_ = abs_path($_); $_ =~ s/.*\///; $_ =~ s/^[0-9- ]*//;} (${DateTimeOriginal;DateFmt('%a. %b %d, %Y');s/\b0+\B//g}) [${directory; $_ = abs_path($_); $_ =~ s#.*/[0-9-]+\s*([^/]+)/[^/]+$#$1#;}]"

This gives me Rothenburg and Wuerzburg, Germany (Wed. Jun 22, 2005) [Jewels of Europe River Cruise] (my test file has a different date than yours).

Bonus points if you fully understand my changes ;)
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

 :o Wow! I am highly impressed Hayo. I just tested it, and your code worked perfectly. I think you deserve the bonus points, not me.

I'll have a look at what you did soon, but forgive me if I have to rush off and have a quick lie down, after studying it.

Cheers,
-Paul

Can't thank you enough for that. I was really expecting I might have been pushing the limits too much with what could be actually be done in a single line of code with ExifTool and Regex. I should never have doubted it!
OS: Windows 11 Pro

mpegleg

Thanks, but I'll pass on the bonus points for now Hayo. I had a quick look, but I think learning Mandarin would be easier. :'(
(It's going to take me a while to fully learn & understand the Regex syntax, but I'll certainly do that one day).


So, here's another one I'll throw at you while you're on fire!

You'll see that I've added a "-" separator between the Current Directory and the Date, so that:


W:\Photos\2010-08-08 Jewels of Europe River Cruise\2010-08-08 Amsterdam, Netherlands\

     2010-08-08 21-12-43 P1030160 [PIX506764] In transit at Singapore Changi Airport.jpg


becomes:


In transit at Singapore Changi Airport  - Amsterdam, Netherlands (Sun. Aug 8, 2010) [Jewels of Europe River Cruise]



It's obviously an if/then conditional case, because if the parsed filename ends up being empty, then obviously I won't want the " - " showing in the result like for:

W:\Photos\2010-08-08 Jewels of Europe River Cruise\2010-08-13 Rothenburg and Wuerzburg, Germany\

     2010-08-13 07-25-22 P1030226 [PIX796859].jpg

Rothenburg and Wuerzburg, Germany (Fri. 13 Aug 2010)  [Jewels of Europe River Cruise, 2010]
^-- No " - " at the start, which is correct for this particular filename.

"-XPcomment<${filename; $_ =~ s/^(?:.*\[PIX\d+\]\s*)?((?:(?!modx).)*)(?:\s*modx.*|\....$)/$1/;s/  +/ /g;s/ +$//;s/^(.+)$/$1 /} - ${directory; use Cwd 'abs_path';$_ = abs_path($_); $_ =~ s/.*\///; $_ =~ s/^[0-9- ]*//;} (${DateTimeOriginal;DateFmt('%a. %b %d, %Y');s/\b0+\B//g}) [${directory; $_ = abs_path($_); $_ =~ s#.*/[0-9-]+\s*([^/]+)/[^/]+$#$1#;}]"



---> So,... is it easy to add a conditional case to this, (on 1 line also)?? Pretty please. Make it so :)


Obviously I'll need to learn the if/then syntax (or however you do it in Regex) because it would be very good in cases like this, where it would probably make better sense to just name that particular photo as:

"In transit at Singapore Changi Airport..." without the Amsterdam, Netherlands part, as we haven't arrived in The Netherlands yet. If we had... I'd be sure to say... G'day Mate, and Thanks!  ;)






ps. I can imagine getting very fancy so that I could base the conditionality ("is that even a word? The spellchecker says no") on various flag characters in the filename, (or perhaps even a metadata tag) as to what would finally be output... but, I'm getting ahead of myself, and that can wait for another... lifetime.  Baby steps Paul, baby steps first!





EDIT(s):
I can't believe whoever wrote those Regex instructions truly expect any n00bs to make sense of that page. I got a headache just quickly browsing it. eek!

Now Googling... "Regex for Dumb & Dumber Dummies"... here I come ;D

Oh. regular expressions 101... I have a sneaky feeling you guys have been giving that site a workout. 8)

That's such a cool site for learning and testing out Regex. Nice. (EDIT: Well it will be once I figure out how to drive the thing).

Also, RegExr is excellent as well, but I guess these are regular tools you guys all use anyway?

I'm also learning a lot from this YouTube video. It has some great Regex info for any other n00bs like me that might come across this post one day.

...and don't worry, I know we're starting to stray into the territory of pure Regex, rather than discussion and help  surrounding ExifTool, so I'll try not to ask for too many Regex type questions from now on. Promise I will try.


     ExifTool + Regex = Awesomeness!
OS: Windows 11 Pro

mpegleg

Hi all.

I've been experimenting with the "-if" operator, (Powerful! :D ) but I need a little help please with how I'd place it in the correct position in the string.

This entire code gets executed and works (or at least does what I currently want):

"-XPcomment<${filename; $_ =~ s/^(?:.*\[PIX\d+\]\s*)?((?:(?!modx).)*)(?:\s*modx.*|\....$)/$1/;s/  +/ /g;s/ +$//;s/^(.+)$/$1 /} - ${directory; use Cwd 'abs_path';$_ = abs_path($_); $_ =~ s/.*\///; $_ =~ s/^[0-9- ]*//;} (${DateTimeOriginal;DateFmt('%a. %b %d, %Y');s/\b0+\B//g}) [${directory; $_ = abs_path($_); $_ =~ s#.*/[0-9-]+\s*([^/]+)/[^/]+$#$1#;}]" -if "$comment eq "1""

based on whether comment="1"

...but, I only want the " - " to be added to the string if that condition is met. Any ideas how I'd structure the whole line to achieve that?

The " - " that I want to conditionally appear, can be seen in the code here: ...} - ${directory; use Cwd...

The conditional "-if" at the moment is at the very end, which is obviously not where I want it.

Thanks,
-Paul

ps, Don't worry too much about the particular metadata tags I'm using (eg. comment + XPComment)... they are just for this example only, for simplicity. I'll be using more robust ones in reality.
OS: Windows 11 Pro

StarGeek

The location of the -if option can be anywhere in the command as long as it doesn't interfere with previous parts of the command.  For example, if you were using the -o (outfile) option, you couldn't place it between the -o and the secondary parameter of that command.

An exiftool command isn't like an IF statement in a programming language.  It affects the whole command, not just the stuff that comes after it.  If the -if returns false, no part of that command will be executed.  Exception: See the -execute option.  Exception to the exception: See the -Common_Args option.
"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

Hayo Baan

Paul,

I don't understand your -if (what's it for?) but to only have the " - " if there's really something following it, is easy:

"-XPcomment<${filename; $_ =~ s/^(?:.*\[PIX\d+\]\s*)?((?:(?!modx).)*)(?:\s*modx.*|\....$)/$1/;s/  +/ /g;s/ +$//;s/^(.+)$/$1 /}${directory; use Cwd 'abs_path'; $_ = abs_path($_); $_ =~ s/.*\///; $_ =~ s/^[0-9- ]*//; $_ =~ s/^(.+)$/ - $1/;} (${DateTimeOriginal;DateFmt('%a. %b %d, %Y');s/\b0+\B//g}) [${directory; $_ = abs_path($_); $_ =~ s#.*/[0-9-]+\s*([^/]+)/[^/]+$#$1#;}]"

I removed the fixed part " - " and added it conditionally by using $_ =~ s/^(.+)$/ - $1/; inside the part that makes up the second part.

Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

Hi Hayo. Thanks for helping. I really appreciate it.

Dont worry, It's hard to explain these things sometimes, and this is starting to get a little bit complex, as I move ahead with my "scenarios". ;).


I'm not quite sure what you did then, but I don't think I notice any difference in the output.


(Note: The directories, photos, and metadata tags used in the following example are all chosen simply for testing purposes).

Here's a pic that might help me explain the current problem better:



Note the "-". I don't want that to appear when the filename parses as an "empty" string.



Now, provided I can get that pesky dash to disappear,... this all got me thinking further (perhaps way too far ahead)...

What if I could control what was to be output, based on what was contained in:

Scenario a). In a metadata tag. Such as the "0" or "1" that you'll see in my Subject column (XPSubject)

=or=

Scenario b). A Filename contained a certain key character string such as for example..."-flag1-"

I could then setup my files, so that they'd automatically be renamed as I want, based on whether a). XPsubject="1"

=or=

b). The filename contains "-flag1-".

Do you see what I'm getting at? That would be very powerful, because it would be global for my entire photo collection, if I setup one of those flag situations correctly. That way, I wouldn't have to worry about what the Directory structure was, or how the filenames were constructed.

The file renaming would be performed based independently on what the Directories and/or filenames were,
BUT I'd also be able to have exceptions when it struck those filename Flags.

Note: I'd only use one or the other of those scenario methods, but I haven't decided which one yet, as I'm still mulling over these ideas, to ascertain what might work best.

I "think" this is where I need to take advantage of the -if" conditional operator.


I've just decided I'm going to mull over this for a little bit longer, as I'm obviously still a bit muddled in my thinking about the Regex vs "-if" scenarios. Leave that with me for now. :)

Cheers
-Paul

ps. That damned "-" still has to disappear though. That's priority #1  ;D
OS: Windows 11 Pro

Hayo Baan

Ah, the front part was what could be empty, I thought it was the second part...

Try this:
"-XPcomment<${filename; $_ =~ s/^(?:.*\[PIX\d+\]\s*)?((?:(?!modx).)*)(?:\s*modx.*|\....$)/$1/;s/  +/ /g;s/ +$//;s/^(.+)$/$1 - /}${directory; use Cwd 'abs_path'; $_ = abs_path($_); $_ =~ s/.*\///; $_ =~ s/^[0-9- ]*//;} (${DateTimeOriginal;DateFmt('%a. %b %d, %Y');s/\b0+\B//g}) [${directory; $_ = abs_path($_); $_ =~ s#.*/[0-9-]+\s*([^/]+)/[^/]+$#$1#;}]"
Hayo Baan – Photography
Web: www.hayobaan.nl