ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: Richhard on April 09, 2023, 07:00:32 PM

Title: Write location tags based on directory name
Post by: Richhard on April 09, 2023, 07:00:32 PM
Hi,

Exiftool is great - so much better than using Adobe Bridge to set metadata.  However I am still new to Exiftool and not getting very far very fast.

All my photos and videos are in a directory structure:
C:\United States\California\Los Angeles
C:\United States\Illinois\Chicago
In Windows Powershell how would I write -City -State -Country tags based on this directory structure?

Also I am wanting to use Exiftool to find all my kids sports photos.  How can I move all files created on any Saturday morning from 9am to 11am to a new folder?

Thanks for any assistance, Rich
Title: Re: Write location tags based on directory name
Post by: Phil Harvey on April 09, 2023, 09:20:53 PM
Hi Rich,

It would be better if your country albums were in a common folder rather than in the root directory, then you could process them all at once by specifying the parent directory.  Instead, this command does what you want only for the "United States" pictures:

exiftool "-xmp:city<${directory;my @a=split '/', $_; $_=$a[-1]}" "-state<${directory;my @a=split '/', $_; $_=$a[-2]}" "-country<${directory;my @a=split '/', $_; $_=$a[-3]}" -r "C:\United States"

Note that I needed to specify "xmp:" for city but not the other tags because there is a city tag in IPTC IIM that would otherwise take priority.

This command will save "_original" files for each file that is edited.

You ask a lot for your second task.  This is possible but I don't have enough time to work up a command to do that for you right now.

- Phil
Title: Re: Write location tags based on directory name
Post by: Richhard on April 10, 2023, 02:55:41 AM
Wow Phil,
Thanks so much for the quick reply.  I will put the photos in a common folder:
C:\media\photos\United States\California\Los Angeles
Would that simplify the command you wrote? (I still have not got the hang of writing regex)

Could an if statement work by combining the date format code %w (weekday number 0-6) and %H (24-hour time 00-23)? 

Much appreciated - Rich
Title: Re: Write location tags based on directory name
Post by: Phil Harvey on April 10, 2023, 06:56:52 AM
Hi Rich,

The command is the same but now you can write the files for all countries:

exiftool "-xmp:city<${directory;my @a=split '/', $_; $_=$a[-1]}" "-state<${directory;my @a=split '/', $_; $_=$a[-2]}" "-country<${directory;my @a=split '/', $_; $_=$a[-3]}" -r "C:\media\photos"

Here is your Saturday command.  Your suggestion makes it easier than I was thinking:

exiftool -if "$createdate >= 60900 and $createdate <= 61100" -d "%w%H%M" -directory="c:/move/here" -r DIR

- Phil
Title: Re: Write location tags based on directory name
Post by: Richhard on April 10, 2023, 08:28:07 PM
Wow - both of those work perfectly!

I used Advanced Renamer to first move the photos into country/state/city folders and then this Exiftool command to assign the location tags.    (Earlier I was using Advanced Renamer to rename files but prefer to do this in Exiftool now as it allows more control)

Selecting photos by 'day of week' and 'time of day' is really elegant and powerful.  (Note- I had to remove first "0" as weekday is only 1 digit).
So easy to find the sports photos, sunrises, Friday evening snaps, work photos and then apply the right subject keywords in metadata.

I have thousands of photos to sort and spending this time getting the workflow and processing sorted using Exiftool will save so much work. 

Thanks soooo much.  Rich
Title: Re: Write location tags based on directory name
Post by: Phil Harvey on April 10, 2023, 09:29:13 PM
Quote from: Richhard on April 10, 2023, 08:28:07 PM(Note- I had to remove first "0" as weekday is only 1 digit).

Ooops. Sorry.  Right.  I have fallen into this trap before.  In Perl, a number that starts with "0" is octal (base 8).  An unfortunate feature in my opinion.  I've edited my post to remove this.

- Phil

Title: Re: Write location tags based on directory name
Post by: Martin Z on April 10, 2023, 10:13:24 PM
Hi Phil, I would also echo both of Richard's comments namely:
(1) While I've been reading and dabbling on/off for a while I am still an EXIFtool newbie so apologies in advance for any dumb questions I may ask!
(2) I've been looking at numerous tools to sort out document/image/video/audio metadata and have to say EXIFtool is far the best I've come across (even better at say editing video metadata than some of the dedicated video metadata tools I tried!) so thanks for all the amazing work you and others have put into it! 👍🏼

If I may, I just wanted to ask a quick question on the code you provided above as have seen similar examples on other questions/posts, however as it's not a language I recognise I am not clear if/how I can use it myself...

exiftool "-xmp:city<${directory;my @a=split '/', $_; $_=$a[-1]}" "-state<${directory;my @a=split '/', $_; $_=$a[-2]}" "-country<${directory;my @a=split '/', $_; $_=$a[-3]}" -r "C:\media\photos"

Please can I ask...
Title: Re: Write location tags based on directory name
Post by: Phil Harvey on April 11, 2023, 09:42:37 AM
Hi Martin,

Thanks for your kind words.

The command I gave is for Windows cmd shell.  The single/double quotes would need to be swapped for any other O/S or shell.

The ExifTool advanced formatting expressions are standard Perl code.  I don't explain Perl, but this feature is documented here (https://exiftool.org/exiftool_pod.html#Advanced-formatting-feature).

- Phil
Title: Re: Write location tags based on directory name
Post by: Martin Z on April 20, 2023, 07:40:19 AM
Hi Phil, you're welcome!

Thanks for the clarification on the EXIFtool commands/syntax -- ah, I thought cmd/batch initially, but ruled that out because of the '$' and '@', I should've trusted my gut! :)

I've been reading through the advanced formatting feature (https://exiftool.org/exiftool_pod.html#Advanced-formatting-feature) documentation (thanks for the link) and think I have got my head around some of it but may need to ask 1-2 follow-ups if that's OK... However, I'll probably do that later in the week, after I've done some more reading (and in a separate post as don't like to 'cross-contaminate' threads where possible).

Thanks again!