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

Hello.

I have a one-off need to extract part of many Windows 10 JPG photo filenames, and copy them into an Exif tag, but unfortunately I'm not up to speed with Regex yet.

I use a "mildly" random identifier in my photo filenames that always looks like this: [PIXxxxxxx], which makes for easy searching.

The filenames do not contain any foreign or unusual characters, but the [PIXxxxxxx] could appear anywhere in the filename.



Here's an example Filename...

This is a test [PIX817263] with the identifier located anywhere in the filename.jpg



Each filename contains [PIXxxxxxx] at an unknown location in the filename, and there are always 6 numerals after "[PIX" represented here as xxxxxx, that are random, ending with a "]".


So, what ExifTool Regex expression would I use to copy the [PIXxxxxxx] (with the brackets included) into (for example) the JPEG Comment tag (ie. comment) for each individual file in a Windows folder?

In summary, just to be clear, using the example I gave above, I would like to have [PIX817263] placed into the Comment tag for that particular jpg file.


I know I could study Regex and finally learn how to do it, but I'm very short on time, and it is only a one-off situation. Can any REGEX experts help please?

Regards,
-Paul

ps. There's no need to explain any examples you may give, unless you want to!

Once I've tested the output, I'll happily try and work backwards to gain an understanding at how the Regex expression works. Thanks!
OS: Windows 11 Pro

StarGeek

The brackets are special characters, so they have to be escaped.  PIX is a straight match, though if there is the possibility of a lower/mixed case pix, then that needs to be taken into account.  Digits match with \d and the fact that there are six of them can be matched by adding {6}.

So to copy that into the Comment field, you could use
"-Comment<${Filename;m/(\[PIX\d{6}\])/;$_=$1}"

This captures the string you want and replaces the value of Filename with the captured string.  This is case sensitive, so if you need it to be case insensitive, add an i after the last slash and before the semicolon.

I will advise, though, that the jpeg Comment field is fragile and there are lots of software out there that will overwrite it.
* 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

StarGeek. Thank you so much! You are a legend in your own time ;)

That worked perfectly!

Yes, I have the PIX as always Uppercase, and I was only using the Comment tag as an easy example, and I'll be using other tags to safely store it away. Thanks for your advice. I wasn't expecting an answer so soon!

-Paul  :)
OS: Windows 11 Pro

mpegleg

StarGeek. Following on from this, I've got 2 similar but separate slightly more complex scenarios now, that I hope you may also be able to help me with.

Still using the Regex code as before, and in the same operation, how would it need to be modified, to allow the following:


1). I may want to append some simple text to the copied tag. This text would be the same for a particular folder of photos. So, let's say (as a test) I wanted to add the phrase "Test 123 " in front of all of my PIX ids, and " xyz" to the end.


So, using my previous example, instead of just [PIX817263],

I'd end up with the tag Comment containing:Test 123 [PIX817263] xyz


------------------------------------------------------------
but ideally this following scenario is what I'll be requiring...

2). I have the initials LW in a tag called "OwnerName".

I'd like to append that (ie. "LW ") to the front of the PIX id.

ie. I'd end up with the tag Comment containing:LW [PIX817263]


I've experimented a bit with this, but I'm not making much progress thus far.

Regards,
-Paul
OS: Windows 11 Pro

Hayo Baan

This shouldn't be too hard:

To add some text to the Comment tag: exiftool "-comment<${comment; $_ = 'PUX ids ' . $_ . ' TEXT'}" FILE
To the text from OwnerName to the Comment: exiftool "-comment<${comment; $_ = $ownername . ' ' . $_}" FILE

(the . is the Perl string concatenation operator)
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

Hmmm. Neither of those are working for me Hayo Baaan.

In Scenario #1, I want the PIX id to be extracted from the filename, and copied to the tag Comment, with the beginning and end phrases added.

So, from my first posted example (using text "Test 1 2 3" and "xyz"):

This is a test [PIX817263] with the identifier located anywhere in the filename.jpg

I would end up with Comment tag: Test 123 [PIX817263] xyz

I'm guessing from your example, that the concatenation simply must to be done in a secondary step from the example that StarGeek quoted??? If that's the case, then that's fine, but it might be easier for me to understand if you could please show me the exact usage, using my example here.


------------------------------------------------------------

As for Scenario #2, with ownername tag="LW" I tried:

exiftool "-comment<${comment;$_=$ownername.' '.$_}" FILE

but it didn't end up modifying the comment tag.
OS: Windows 11 Pro

Phil Harvey

Hi Paul,

Try this:

"-Comment<Test 123 ${Filename;m/(\[PIX\d{6}\])/;$_=$1} xyz"

You may replace "Test 123" with "$ownername" to insert the value of the OwnerName tag in place of "Test 123".  In the case where OwnerName doesn't exist, you can fall back to a default (eg. "Default Name") like this:

exiftool "-Comment<Default Name ${Filename;m/(\[PIX\d{6}\])/;$_=$1} xyz" "-Comment<$ownername ${Filename;m/(\[PIX\d{6}\])/;$_=$1} xyz" DIR

- 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

Thanks Phil!

That solved it  :)

Much appreciated all of you for your help. Now I really can get to work. I only have a mere 43167 photos to process. Ah fun.  :-\
OS: Windows 11 Pro

mpegleg

Ok. So one final thing, (and it's only minor in that I'm just trying to understand the "why?", and I'm sure it's been discussed many times before),... and, I'm guessing it has to do with the default order of ExifTool processing...

(Let's assume that the OwnerName tag starts off with a value of 'MD').

If I then use the following command to modify 3 tags, setting the new OwnerName to 'LW',

exiftool -OwnerName=LW "-Comment<$OwnerName ${Filename;m/(\[PIX\d{6}\])/;$_=$1}" "-Number<${Filename;m/(PIX\d{6})/;$_=$1}" FILE

I find that the old OwnerName of MD will be used in the new Comment tag instead of the new OwnerName of LW.

ie. The result is that...

tags...
OwnerName now contains: LW
Comment now contains: MD [PIX817263]    <-- I expected LW [PIX817263]
Number now contains: PIX817263



So, my question is... Is there a simple way around this apart from splitting the commands into 2? like this...


exiftool -OwnerName=LW FILE
exiftool "-Comment<$OwnerName ${Filename;m/(\[PIX\d{6}\])/;$_=$1}" "-Number<${Filename;m/(PIX\d{6})/;$_=$1}" FILE


which does give the correct results as follows...

tags...
OwnerName now contains: LW
Comment now contains: LW [PIX817263]
Number now contains: PIX817263


ps. It's not a big problemo if I have to use the double command method. It just would be a bit simpler to use with some of the GUI programs, if I could just use the single one line command.

[EDIT: I just read this: FAQ 22 so I guess that kind of explains it].  :o

Phil... You can just answer me with Read FAQ 22 if that does apply in this situation. I'm guessing it does. So 2 command lines it is!  :-\

pps. Even so... I still want it in one  :'(
OS: Windows 11 Pro

Phil Harvey

I'm glad you found FAQ 22.

You can do it in one line very easily:

exiftool -OwnerName=LW "-Comment<LW ${Filename;m/(\[PIX\d{6}\])/;$_=$1}" "-Number<${Filename;m/(PIX\d{6})/;$_=$1}" FILE
...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

Oh. I see what I was doing. D'oh! lol. If the new value is already known then yeah, there's obviously no need to reload it indirectly.  :-[

I blame Regex. It has that effect on my brain.

Thanks again Phil. ExifTool is brilliant!
OS: Windows 11 Pro

mpegleg

Using this...

"-Comment<LW ${Filename;m/(\[PIX\d{6}\])/;$_=$1}"

I just noticed that the output tag will contain this: LW m/(\[PIX\d{6 if the filename does not contain one of my PIX ids. (Ideally every photo filename should contain a PIX id, but there are some I haven't got around to updating just yet).

How would I go about making the tag just contain "LW" or "LW " ?    (Note: Blanks/Spaces don't concern me).

... or perhaps even better would be an error code inserted into the tag like "LW NO PIX ID"

To keep things simple, it doesn't really matter to me what the tag would contain upon lack of a PIX id, as long as it doesn't appear as gobbledegook like before, as it will be visible on a TV screen, overlaid onto the photo.


OS: Windows 11 Pro

Phil Harvey

Right.  Try this:

exiftool -OwnerName=LW "-Comment<LW ${Filename;$_ = m/(\[PIX\d{6}\])/ ? $1 : 'NO PIX ID'}" "-Number<${Filename;$_ = m/(PIX\d{6})/ ? $1 : 'NO PIX ID'}" FILE

- Phil

Edit: Fixed incorrect double quote.  ooops
...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

mpegleg

OK. If I can just add a quickie to this posting. Hopefully it is an easy one...

I just need a quick & dirty regex expression to copy the filename into the comment tag without the known extension ".jpg", so there is no need to test for any other possibilities concerning uppercase, spaces, unusual characters ,etc, etc.

In other words, it just has to simply chop off the last 4 characters, and do nothing else, but copy all the characters of the Windows 10 filename before the".jpg" into the comment tag.

I know there are config files, etc, but I just want to use it here where the extension is known to always be ".jpg"



eg. Filename is:This is a test.jpg

I would like the comment tag to contain:This is a test



Thanks,
Paul
OS: Windows 11 Pro