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

Thanks Hayo. Don't give yaself a headache over it, but I really do appreciate your help.



Ok. Now that I think I've de-scrambled my brain... I remember why I thought we'd have to use the "-if" condition.

SCENARIO:

Only "some" of my Directories have this "Main Directory/Sub Directory" structure for a particular group of photos ... mostly Vacation Photos. eg. 2010-05-01 Russian Vacation/2010-05-01 Moscow/*.jpg

These however are the exception. Most are just separate Directories of random daily event photos all in date order through the years. Eg).

Directories...
2001-10-05 Pictures of our lovely cute little kitten
2004-08-14 More pictures of the stupid cat
2015-07-08 Even more pictures of the now decrepit old flea-infested moggy
2017-01-01 Fluffy's garden burial service - sad

So, jokes aside... I don't "always" want the higher Directory name to be included in the output tag. In fact for most of the Directories, I won't want it.

Now, I don't expect, and I'd rather you didn't help me with the intricacies of setting up the syntax to handle that, but I would like some advice on whether you think I should be able to do that with standard ExifTool + Regex, as we've been doing so far, or would I need to get fancier and use the "-if" Conditional structure.

This is where I was thinking I could use a metadata "flag" (ie. Tag value="1") to trigger the condition.

Then, I'd only have to set those particular Vacation photo groups with that respective tag set=1, and everything would be automatic.


How complex would this be to achieve?
OS: Windows 11 Pro

mpegleg

Aha. You edited your post with the corrected code! Yay. Thank you. I've been "hanging out" waiting for it, because I couldn't seem to work it out myself, but I've just tested it and it seems to work fine now. :)

Lucky I saw your modified post though, Hayo.  ;) That's a somewhat "risky" way of posting, because I don't get any notification of any change. I usually have my browser set to alert me of any new posts, but it won't of course alert me of modified posts.

It was pure luck that I noticed that what you had written looked different from before... ie. You'd removed the "I don't understand part". Anyway, just a friendly tip there, because I was really looking forward to the modified code, even though I was trying to stay polite and calm. lol.
Now I can really start do some serious work with my photo collection organising.

Thanks again Hayo,
and I much, much appreciate all your efforts in helping me with that code!

-Paul

EDIT: Sorry. I may have just missed seeing your previous post. Glad I did though! As I said... It' s working really well. now,
OS: Windows 11 Pro

mpegleg

Hayo, I just tested your new and improved super-code, and I can confirm,
that out of a thousand or so of my pics, so far there were no surprises. Worked like a dream. :D

You're a legend. It will save me so much time like you won't believe!

Thanks again, (I sure seem to be doing a lot of thanking lately, but I really am happy)
-Paul
OS: Windows 11 Pro

mpegleg

Hi.

As this has huge practical application value for me, I'm just experimenting a little with the -if conditional option, with multiple commands on the one line.

Following is a very basic example, just to simplify what I'm trying to achieve with the -if option.

It's a typical "if" "then" "else" situation, but because there is no "else" operator, I'm a bit unsure how to construct it.

Here I'm using the value contained in XPSubject as a kind of "flag". It will contain either the exact value of "1" =or= "something" else, (which would be unknown and irrelevant).


-if $XPSubject eq "1" -p $directory

-if not $XPSubject eq "1" -p $filename



Obviously the commands will be much more complex in actuality,

but I'd like to put these commands on the same line. Obviously the -if option applies to the whole line, so I'm "guessing" I have to use the -execute option somehow. I tried putting that after my first line above, followed by the second "-if" statement, but that didn't work.

How would I construct the full command, to enable both "-if"s to occur one after the other, on just one line? Is that possible, or does it absolutely require 2 separate exiftool.exe command statements?



Also, I was just thinking some more (dangerous I know!) about extending this idea, with more "flag" values, thus:

-if not $XPSubject eq "1" -p $filename  <-- so most of the time it will do this operation
-if $XPSubject eq "1" -p $directory
-if $XPSubject eq "2" -DateTimeOriginal

Once again, how would I run it all on one line? Note: To keep things simple, the execution sequence would not be important here.


EDIT: I guess I could write a simple batch file to accomplish the same thing, but It still leaves me wondering if it's possible to put them all on one ExifTool command line :-\
OS: Windows 11 Pro

StarGeek

exiftool -if "$XPSubject eq '1' " -p $directory -execute -if "not $XPSubject eq '1' " -p $filename -common_args /path/to/working/directory

Assuming that you're processing the same directory, you have to put the directory path at the end after the -Common_Args option.  You also want to put anything else that would be in common to both commands at this point, such as -r.

Any thing before the -execute is combined with the -common_args options and the command is run.  The it starts the second part, combines with the common args again, and since there isn't anything left, it runs all that and exits. 

* 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

Thanks very much StarGeek.

I'll have a good thorough look at that, and have some fun playing around with it soon.

Cheers,
Paul
OS: Windows 11 Pro

mpegleg

OK. I got it working so well now, using about 5 sequential commands. That's just what I'd hoped all along to be able to do, as it opens up a whole new realm of completely automating all my file and metadata tag renaming. Brilliant :D

Once again... many thanks to StarGeek, Hayo & Phil.

Cheers,
-Paul

ps. It was a long, and difficult journey (probably more so for you folks putting up with all my "science-project-homework-type" questions... but I got there in the end). As they say... All's well that ends well. :)
OS: Windows 11 Pro

mpegleg

I know you are "supposed" to use a format file to better achieve the formatted text output, which I have been doing, but I was just wondering (as I always do - If ya don't ask, you'll never know)...


eg). In the following test scenario (with XPSubject="1") everything will output on it's own new line:

-m -p "XPSubject: " -p $XPSubject -p "    Creation Date: " -p $DateTimeOriginal FILE

XPSubject:
1
    Creation Date:
2019:06:11 14:04:20



I'd prefer:

XPSubject: 1     Creation Date: 2010:08:14 09:51:00


Is it possible to suppress the CR/LR after each -p option?

If not, then yes, I'll stick with the format file, but it would be useful for the occasional quick and dirty little runs where I didn't want to have to bother.


I guess what I was hoping for, is another "switch" option that tells the -p option not to CR/LF after the next output ends.

Could it be the -w option I'm looking for? I'm not really familiar with that yet. I only want console output though.
OS: Windows 11 Pro

Hayo Baan

Just bring everything you want printed into one string:
-m -p "XPSubject: $XPSubject Creation Date: $DateTimeOriginal"
Hayo Baan – Photography
Web: www.hayobaan.nl

mpegleg

Oh Hayo. Now that was just too darn simple. Laughing at myself.

Thank you!!!!

Sometimes the complete obvious is staring one in the face!
OS: Windows 11 Pro

mpegleg

Help please! My Regex knowledge is unfortunately still too basic for this...

I'd like a simple Regex expression to match the 1st, 2nd, 3rd, 4th, or 5th Character in a 5-character string. Let's call it Code5. eg. PC101

This is so that I can do an "-if" option condition on it, to then perform an ExifTool command.

ie. I was thinking of having a 5-character alphanumeric code that would allow the inspection of a particular character in the 5-character string (that will be stored in a specific metadata tag), to determine whether to perform a specific ExifTool command, or not.

eg,

Something along the lines of:

-if "1st character of the Code5 eq 'P' " ...do this ExifTool command... Perhaps output the Parent Directory name
-if "2nd character of the Code5 eq 'C' " ...do this ExifTool command... Perhaps output the Current Directory name

The part I need is the Regex that can obtain the specific alphanumeric character, based on it's position in the string ie. "Character x of the Code5"

Hopefully that makes sense. If not, let me know, and I'll try to explain it better.

Many Thanks,
-Paul
OS: Windows 11 Pro

Phil Harvey

This can be done with a regex:

-if "$code5 =~ /^P/"

-if "$code5 =~ /^.C/"

or without a regex:

-if "substr($code5,0,1) eq 'P'"

-if "substr($code5,1,1) eq 'C'"

(Assuming the tag name is "Code5")
...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

Fantastic. That was quick!

Thanks Phil.

I'll give it a whirl, and see what works best. :)
OS: Windows 11 Pro

mpegleg

Phil, I searched for some info on the correct usage syntax for the "substr" operator, but couldn't find it.

Would you mind please pointing me in the right direction for it?

Much appreciated,
-Paul
OS: Windows 11 Pro

Phil Harvey

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