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

Hi. I'm locked out from modifying my own posts here, (as the time limit for edits has passed), so can one of the mods be so kind as to please edit it for me, as I realize that it won't work as is, under Windows. Strange thing is, it worked using ExifTool Mixer. :-\

Anyway, the minor (but important) correction required, is in my very last post.

-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

should be:

"-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

I did some tests, and apparently the position of the double quotes at the start is very important here.
(In future I'll have to make sure I do a few more tests using cmd, batch and ExifMixer, before posting. These files were also on a mapped network drive, so that can obviously cause Windows path issues).

Many Thanks!

ps. I'll leave you all in peace now

Cheers,
Paul.
OS: Windows 11 Pro

Hayo Baan

Yes, the position of the quotes is important or else the shell will interpret things differently (e.g. the < will be seen as input redirection, not as part of the argument to exiftool, when it not in quotes). Anyway, glad everything has worked out for you in the end.
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

Hi Hayo Baan.

Do you have the auth to modify that post, or is Phil the only one authorised to do that? I hate leaving bad code lying around that is clearly wrong. Some poor sod is likely to come across this thread, and cut & paste the example, and end up with a headache for nothing.
OS: Windows 11 Pro

Hayo Baan

Can't you modify the post yourself? Afaik you should always be able to edit your own posts (I am on another forum that uses the same software and there I have edited post from years back)? Anyway, as moderator I was able to make the change (moved the double quote in front). Was that all that needed to be changed?
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

Well yes, I do normally modify my own posts (too many times. lol), but there must be a time limit, because the "Modify" button has disappeared on all my posts in this thread except for the last 2 I have just made. I noticed this before. I think the limit has been set for 24 hours or just under.

Thanks for changing it anyway. Yes that's all for now. I have another Regex question coming up soon. I'm just trying to figure it out myself first, but so far not having much success. Regex is hard (for n00bs). I'll keep working on it for now, and I'll ask if I can't figure it out.


I was pretty familiar with SMF Forum software. I was an admin of a site using it, but that was quite a few years ago, so I'm a bit out of touch with it. Yes you can set a time limit on how long people can make edits. I got "the time limit for edits has passed" message when I tried to use another tab that I had open, so I know that there is definitely a time limit set. (Perhaps the time limit has been set up in tandem with the number of member posts. From memory, you can set it up that way too). SMF Forum software has almost as many customisable options as ExifTool ! ;)

Cheers,
Paul
OS: Windows 11 Pro

mpegleg

Ok. I surrender on this one. Regex is not much fun when you are sleep-deprived.

Anyway, I know it's some relatively easy Regex code (well "easy" when you know how!).

I use the term modx as a search term because it's pretty unique (as is PIX).

So, some of my "edited" photos contain the term "modx" somewhere in the filename.
Anything after the modx is for my eyes only, as it is only editing babble.



What I'd like to do, is strip [PIXxxxxxx] as before, (it may appear anywhere in the filename), but I also want to strip out the " modx" and anything after it, including the space character before the modx up to and including the filename extension.


Example filename:

I don't want this text but [PIX172643] I do want this text modx but I don't want this.jpg


so that the comment tag will only contain:

I do want this text

It would also have to take into account that sometimes the modx will not be in the filename. (Most of the time it won't be).

Assume that the PIX code will ALWAYS be somewhere in the filename, even at the very end, which should mean that only in this case, the comment tag should be forced empty, OR even better it could have "NO PIX ID" stored as an error flag, as I really want every single photo to have a PIX code.

*sigh* is that a brain drain or what? :-\

Thanks,
Paul

OS: Windows 11 Pro

Hayo Baan

A regexp to only get the I do want this text part is: s/^(?:.*\[PIX\d+\]\s*)?((?:.(?!modx))*?)(?:\s*modx.*|\..{3,4}$)/$1/

The "hardest" part was to allow for the PIX and modx parts to be absent for that I made use of a negative look-ahead assertion ?! as well a non-greedy capture *?. I also allowed for 3 or 4 letter extensions. (and in case you wonder what ?: is, that means it is a non-capturing group, this makes it a bit faster and we don't care about the content anyway).
Of course, splitting this into two regexps would have been easier, but I liked a challenge ;)

To test this e.g. use exiftool "-testname<${filename; $_ =~ s/^(?:.*\[PIX\d+\]\s*)?((?:.(?!modx))*)(?:\s*modx.*|\....$)/$1/;}"
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

Thank Hayo. I'll test it out. Sorry, I added the bit to my post where I said... assume that PIX will ALWAYS be present, but... if it could flag an error, that would be even better.

If you need to change it, leave the other code as well because it's a very good learning exercise for me.
OS: Windows 11 Pro

Hayo Baan

No, there's no change needed if PIX is non-optional (though in that case you can remove the (?: at the front and the )? at the end of (?:.*\[PIX\d+\]\s*)?, making it it a bit simpler).
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

There were a lot of errors produced, however the result was perfect in the first trial, but did not remove the "modx" in the 2nd and 3rd trials. I hope you can read the attached cmd screenshot. (You may need to download it)...



I just realised the first trial was missing a double-quote after the filename, but that one worked anyway, probably because I included the first double-quotes, and I was lucky that there were no other args or commands after the jpg.
OS: Windows 11 Pro

Hayo Baan

Hmm, these errors indicate either you have a problem with your exiftool installation, or there's a typo in the code you used that makes the exiftool code trip (at a later point). Actually, given the "missing curly brace" perl error, that seems likely. Make sure, for instance, the double quotes aren't smart quotes; that will make things fail on the command-line.

Next time instead of posting a screenshot, simply copy/paste the content of the screen into a [ code ] [ /code ] block, that way I can copy and use the exact command you typed to see what I get here with that exact same command.


Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

Quote from: Hayo Baan on June 17, 2019, 05:20:12 AM
Hmm, these errors indicate either you have a problem with your exiftool installation, or there's a typo in the code you used that makes the exiftool code trip (at a later point). Actually, given the "missing curly brace" perl error, that seems likely. Make sure, for instance, the double quotes aren't smart quotes; that will make things fail on the command-line.

I don't normally get all those error messages. It's pretty much the first time I've seen any of those. I can test it on another pc if necessary. I simply cut and pasted your code, and added the other bits. There's nothing fancy about my installation, and the exiftool.exe v11.51 file has simply been downloaded, unzipped, and dropped into the same folder along with those test images.


Quote
Next time instead of posting a screenshot, simply copy/paste the content of the screen into a [ code ] [ /code ] block, that way I can copy and use the exact command you typed to see what I get here with that exact same command.


EDIT: OK. I found how to cut and paste the text from the cmd windows. It's a bit different to normal windows.

Bear with me... I'll try to post the text here soon...
OS: Windows 11 Pro

mpegleg

Filename:I don't want this text but [PIX172643] I do want this text modx but I don't want this.jpg


W:\Tests>exiftool -m -overwrite_original "-XPcomment<${filename; $_ =~ s/^(?:.*\[PIX\d+\]\s*)?((?:.(?!modx))*)(?:\s*modx.*|\....$)/$1/;}" "I don't want this text but [PIX172643] I do want this text modx but I don't want this.jpg"
Global symbol "$psiz" requires explicit package name (did you forget to declare "my $psiz"?) at Image/ExifTool/Photoshop.pm line 203, <EXIFTOOL_FILE> chunk 4.
syntax error at Image/ExifTool/Photoshop.pm line 203, near "2) "
Global symbol "$psiz" requires explicit package name (did you forget to declare "my $psiz"?) at Image/ExifTool/Photoshop.pm line 203, <EXIFTOOL_FILE> chunk 4.
Global symbol "$len" requires explicit package name (did you forget to declare "my $len"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
Global symbol "$psb" requires explicit package name (did you forget to declare "my $psb"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
Global symbol "$data" requires explicit package name (did you forget to declare "my $data"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
Global symbol "$data" requires explicit package name (did you forget to declare "my $data"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
syntax error at Image/ExifTool/Photoshop.pm line 205, near "}"
Global symbol "$raf" requires explicit package name (did you forget to declare "my $raf"?) at Image/ExifTool/Photoshop.pm line 206, <EXIFTOOL_FILE> chunk 4.
Unmatched right curly bracket at Image/ExifTool/Photoshop.pm line 208, at end of line
Image/ExifTool/Photoshop.pm has too many errors.
Compilation failed in require at (eval 35) line 2, <EXIFTOOL_FILE> chunk 4.
Can't find table Image::ExifTool::Photoshop::Main
    1 image files updated

W:\Tests>


Result:I do want this text  <-- That's good.



Filename:I don't want this text but [PIX172643] modx but I don't want this.jpg


W:\Tests>exiftool -m -overwrite_original "-XPcomment<${filename; $_ =~ s/^(?:.*\[PIX\d+\]\s*)?((?:.(?!modx))*)(?:\s*modx.*|\....$)/$1/;}" "I don't want this text but [PIX172643] modx but I don't want this.jpg"
Global symbol "$psiz" requires explicit package name (did you forget to declare "my $psiz"?) at Image/ExifTool/Photoshop.pm line 203, <EXIFTOOL_FILE> chunk 4.
syntax error at Image/ExifTool/Photoshop.pm line 203, near "2) "
Global symbol "$psiz" requires explicit package name (did you forget to declare "my $psiz"?) at Image/ExifTool/Photoshop.pm line 203, <EXIFTOOL_FILE> chunk 4.
Global symbol "$len" requires explicit package name (did you forget to declare "my $len"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
Global symbol "$psb" requires explicit package name (did you forget to declare "my $psb"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
Global symbol "$data" requires explicit package name (did you forget to declare "my $data"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
Global symbol "$data" requires explicit package name (did you forget to declare "my $data"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
syntax error at Image/ExifTool/Photoshop.pm line 205, near "}"
Global symbol "$raf" requires explicit package name (did you forget to declare "my $raf"?) at Image/ExifTool/Photoshop.pm line 206, <EXIFTOOL_FILE> chunk 4.
Unmatched right curly bracket at Image/ExifTool/Photoshop.pm line 208, at end of line
Image/ExifTool/Photoshop.pm has too many errors.
Compilation failed in require at (eval 35) line 2, <EXIFTOOL_FILE> chunk 4.
Can't find table Image::ExifTool::Photoshop::Main
    1 image files updated

W:\Tests>


Result:modx but I don't want this  <-- should be blank



Filename:I don't want this text but [PIX172643] modx.jpg



W:\Tests>exiftool -m -overwrite_original "-XPcomment<${filename; $_ =~ s/^(?:.*\[PIX\d+\]\s*)?((?:.(?!modx))*)(?:\s*modx.*|\....$)/$1/;}" "I don't want this text but [PIX172643] modx.jpg"
Global symbol "$psiz" requires explicit package name (did you forget to declare "my $psiz"?) at Image/ExifTool/Photoshop.pm line 203, <EXIFTOOL_FILE> chunk 4.
syntax error at Image/ExifTool/Photoshop.pm line 203, near "2) "
Global symbol "$psiz" requires explicit package name (did you forget to declare "my $psiz"?) at Image/ExifTool/Photoshop.pm line 203, <EXIFTOOL_FILE> chunk 4.
Global symbol "$len" requires explicit package name (did you forget to declare "my $len"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
Global symbol "$psb" requires explicit package name (did you forget to declare "my $psb"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
Global symbol "$data" requires explicit package name (did you forget to declare "my $data"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
Global symbol "$data" requires explicit package name (did you forget to declare "my $data"?) at Image/ExifTool/Photoshop.pm line 204, <EXIFTOOL_FILE> chunk 4.
syntax error at Image/ExifTool/Photoshop.pm line 205, near "}"
Global symbol "$raf" requires explicit package name (did you forget to declare "my $raf"?) at Image/ExifTool/Photoshop.pm line 206, <EXIFTOOL_FILE> chunk 4.
Unmatched right curly bracket at Image/ExifTool/Photoshop.pm line 208, at end of line
Image/ExifTool/Photoshop.pm has too many errors.
Compilation failed in require at (eval 35) line 2, <EXIFTOOL_FILE> chunk 4.
Can't find table Image::ExifTool::Photoshop::Main
    1 image files updated

W:\Tests>


Result:modx  <-- should be blank
OS: Windows 11 Pro

mpegleg

!!! It was those random images I chose that are causing the errors!

Stand by.

I'm running some tests on other images... and so far no errors...
OS: Windows 11 Pro

mpegleg

Hayo. It worked on all tests!

I'm sorry I didn't test other images before. It still doesn't explain why those particular images caused the errors, but I get no errors with the new images I just tried and the results were as hoped.

Thanks, and sorry!

-Paul

ps. Looking at the image I chose, (and I'd just copied it and renamed for the test). It turns out it was a particularly bad choice. I never even looked at the picture to be honest, but now I realise it was a "special" case image. One that has been heavily manipulated by my Uncle and me. It's a really poor quality image and not one that I would normally show anyone. I think it was one I had actually flagged for deletion, but I like keeping cr*p" images just for this very good test case! You can have a copy if you want for testing. It appears to be a very blurry old image of a German castle. My Uncle took it while holidaying there. It had been cropped heavily, and I think I have Photoshopped it recently too.
OS: Windows 11 Pro