Copy parts of filename into tag

Started by thimplicity, November 12, 2017, 09:58:56 AM

Previous topic - Next topic

thimplicity

Good morning everyone,
I am completely new to ExifTool. I found the forum, as I am re-organizing our entire photo collection. I have found a way to get all necessary information into the file name, that has the following structure:

<Year>-<Month>-<Day> - <Country>-<State>-<City>-<Event name> <Increasing number>.<extension>

Example:
2016-03(January)-31 - United States-NC-Raleigh-Zoo visit 0968.jpg

I would like to add the following information as single Tags:

  • Year
  • Month
  • All other information as terms, not single words, so "United States" instead of "United";"States"

So for the example above the file should get the following Tags:

  • 2016
  • January
  • United States
  • NC
  • Raleigh
  • Zoo visit

Based on what I learned to far, I think I need two commands, one for the Date part and one for the item part. I would also like to to copy the name except the number (at the end) into the file title.

It would be great, if someone could help me out!

StarGeek

It's possible to do in a single command (it will look a bit messy), but it does require some questions about the exact format of your filename and some other items.

You're two examples are inconsistent on the spacing.  For example, "<City>-<Event name>" vs "Raleigh - Zoo visit" has spaces around the hyphen in the second example.  If care isn't taken this could lead to a situation where you would have a keyword with "Raleigh " (space at the end) and "Raleigh" (no space), which would be treated as two different keywords.  This can be worked around if there are variances but easier if there aren't.

Does any of the items have a hyphen in them.  For example, if the City was "Winston-Salem" or if the event name had a hyphen in it, those would give inconsistent results.

You want to copy to "Tags".  Are you on Windows and looking under Properties to find Tags?  Odds are what you actually want is Keywords/Subject as the target for your data.

Do you wish to overwrite any previous tags or just add the new information?

Here's the command I came up with.  I've tried to make it account for any inconsistencies regarding SpaceHyphenSpace groupings.  It will add keywords without overwriting any previous ones.  Any stray hyphens in any of the data will give inconsistent  results.
exiftool -Sep "##" "-keywords+<${Filename;s/(\d{4})-\d\d *\(([^)]*)\) *- *\d\d? *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*) \d+\..*$/$1##$2##$3##$4##$5##$6/}" FileOrDir

Example output:
C:\>exiftool -keywords "Y:\!temp\y\2016-03 (January) -31 - United States-NC-Raleigh - Zoo visit 0968.jpg"
Keywords                        : Balloons

C:\>exiftool -Sep "##" "-keywords+<${Filename;s/(\d{4})-\d\d *\(([^)]*)\) *- *\d\d? *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*) \d+\..*$/$1##$2##$3##$4##$5##$6/}" "Y:\!temp\y\2016-03 (January) -31 - United States-NC-Raleigh - Zoo visit 0968.jpg"
    1 image files updated

C:\>exiftool -keywords "Y:\!temp\y\2016-03 (January) -31 - United States-NC-Raleigh - Zoo visit 0968.jpg"
Keywords                        : Balloons, 2016, January, United States, NC, Raleigh, Zoo visit
* 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).

thimplicity

Quote from: StarGeek on November 12, 2017, 12:40:59 PM
You're two examples are inconsistent on the spacing.  For example, "<City>-<Event name>" vs "Raleigh - Zoo visit" has spaces around the hyphen in the second example.  If care isn't taken this could lead to a situation where you would have a keyword with "Raleigh " (space at the end) and "Raleigh" (no space), which would be treated as two different keywords.  This can be worked around if there are variances but easier if there aren't.

I have corrected the inconsistencies in my initial post

Quote from: StarGeek on November 12, 2017, 12:40:59 PM
Does any of the items have a hyphen in them.  For example, if the City was "Winston-Salem" or if the event name had a hyphen in it, those would give inconsistent results.

Great question and you are of course correct. What other separator would you propose to use, maybe # or $?

Quote from: StarGeek on November 12, 2017, 12:40:59 PM
You want to copy to "Tags".  Are you on Windows and looking under Properties to find Tags?  Odds are what you actually want is Keywords/Subject as the target for your data.

Do you wish to overwrite any previous tags or just add the new information?

I am running Windows 10 and looking for the Tags under "Properties" and then it is called "Tags". I would like to add new tags to existing ones and if possible leave out the ones that are already there

Quote from: StarGeek on November 12, 2017, 12:40:59 PM
Here's the command I came up with.  I've tried to make it account for any inconsistencies regarding SpaceHyphenSpace groupings.  It will add keywords without overwriting any previous ones.  Any stray hyphens in any of the data will give inconsistent  results.
exiftool -Sep "##" "-keywords+<${Filename;s/(\d{4})-\d\d *\(([^)]*)\) *- *\d\d? *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*) \d+\..*$/$1##$2##$3##$4##$5##$6/}" FileOrDir

Example output:
C:\>exiftool -keywords "Y:\!temp\y\2016-03 (January) -31 - United States-NC-Raleigh - Zoo visit 0968.jpg"
Keywords                        : Balloons

C:\>exiftool -Sep "##" "-keywords+<${Filename;s/(\d{4})-\d\d *\(([^)]*)\) *- *\d\d? *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*) \d+\..*$/$1##$2##$3##$4##$5##$6/}" "Y:\!temp\y\2016-03 (January) -31 - United States-NC-Raleigh - Zoo visit 0968.jpg"
    1 image files updated

C:\>exiftool -keywords "Y:\!temp\y\2016-03 (January) -31 - United States-NC-Raleigh - Zoo visit 0968.jpg"
Keywords                        : Balloons, 2016, January, United States, NC, Raleigh, Zoo visit


Thanks a lot. I am a bit confused by the Balloons key word in there. Would this also copy the filename except the number to the Title?

Could you split it up for me into two commands, so that I can understand it better?

StarGeek

Quote from: thimplicity on November 12, 2017, 02:02:50 PM
Quote from: StarGeek on November 12, 2017, 12:40:59 PM
Does any of the items have a hyphen in them.  For example, if the City was "Winston-Salem" or if the event name had a hyphen in it, those would give inconsistent results.

Great question and you are of course correct. What other separator would you propose to use, maybe # or $?

Now this confuses me.  Are your files not already named according to this pattern?  If not and you are creating the filename from other tags, then it's possible that parsing the filename would not be the best solution.  For example, if City is already a tag in the file, it would be better to copy the city name from that tag to the Keywords.  If you already have a exiftool command planned to rename files, it would be better to see that and I could use that to create a command that is easier to read.

QuoteI am running Windows 10 and looking for the Tags under "Properties" and then it is called "Tags". I would like to add new tags to existing ones and if possible leave out the ones that are already there

Ok, then you want to copy to Keywords and/or Subject, depending upon what other software you might be using.  Subject uses the newer standard and would be better if you were also using a program such as Lightroom, but if you were depending upon Irfanview as your picture viewer, it would be better to use Keywords.  Of course, writing to both is also an option.

QuoteThanks a lot. I am a bit confused by the Balloons key word in there.

Balloons was a keyword already in the file.  I was using it to show that this command would add keywords without overwriting any previous existing keywords.

QuoteWould this also copy the filename except the number to the Title?

No, that would require a different command.  Assuming you mean the Windows property of "Title" and you also don't want the extension, that command would be:
exiftool "-Title<${Filename;s/(.*?) +\d+\.[^.]+$/$1/}" FileOrDir

Example output:
C:\>exiftool "-Title<${Filename;s/(.*?) +\d+\.[^.]+$/$1/}"  "Y:\!temp\y\2016-03 (January) -31 - United States-NC-Raleigh - Zoo visit 0968.jpg"
    1 image files updated

C:\>exiftool -Title "Y:\!temp\y\2016-03 (January) -31 - United States-NC-Raleigh - Zoo visit 0968.jpg"
Title                           : 2016-03 (January) -31 - United States-NC-Raleigh - Zoo visit


QuoteCould you split it up for me into two commands, so that I can understand it better?

Not really.  Splitting would require reconstruction to skip past the points skipped.  I'll take it step by step though.  Knowledge of Regular Expressions (RegEx) is very helpful.

-Keywords+< - Skipping the -Sep option for now (I'll come back to it), this tells exiftool that we're going to copy from one tag to another, indicated by the less than sign <.  In this case, copying to Keywords.  The plus sign indicates that the tags are going to be added to any previous keywords and not overwrite them.  This is a special function for List-Type tags and wouldn't work for simple string tags such as Description.

${Filename; - The tag that is being copied, in this case Filename.  The ${ indicate that we're going to do some advanced formatting commands.  The semicolon indicates the end of the tag name and the start of the advanced formatting.

s/ - This is the start of the advanced formatting.  Here, the s/ indicates a RegEx Substitution command.  The full format is s/Search/Replace/Options.  No Options are going to be used here so we'll skip that.

(\d{4})-\d\d *\(([^)]*)\) *- *\d\d? *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*) \d+\..*$ - This is the Search part of the RegEx and it's pretty complex, so I'll break it down further.

(\d{4}) - First, parse out the year and save it.  \d stands for any Digit character and {4} indicates there must be four of them.  This is surrounded by parenthesis to indicate that it's going to be captured and saved, in this case in variable $1 since it's the first capture.

-\d\d *\( - The stuff to be skipped.  A dash, followed by two digits, which is the month number.  The next two characters are a space and an asterisk.  The asterisk modifies the previous entry, in this case a space, and indicates Zero or more of them.  So, any number of spaces after the 2 digit month number.  Then a \(.  The backslash escapes the left parenthesis so it's not mistaken for the start of a capture group.

([^)]*) - The parenthesis are the next capture group, which will be saved in $2 and will hold the Month name.  Then there are brackets.  Brackets are used to match certain characters (Character Class).  But this character class starts with a Caret ^ which negates what follows, which is the right parenthesis.  So [^)] will match any non-right parenthesis character.  This is followed by the asterisk, so it's zero or more non-right parenthesis character, which will be the month.  There are other ways to do this but I didn't want to take the chance that something else like the Event part of the file name might include parenthesis.

\) *- *\d\d? *- * - More stuff to ignore.  \) matches the closing parenthesis after the month name.  Followed by any number of spaces, a dash, any number of spaces.  \d\d? will match the day number.  \d\ will match a single digit and the question mark after the second \d modifies it to indicate 0 or 1 of the previous character.  If all of the files have two digits for the date, e.g. 01 or 05 and not 1 or 5, then the question mark can be dripped.  This is followed by any number of spaces, a dash, any number of spaces.

([ \w]*?) - These will be the capture groups for the next four pieces of data, the Country, State, City, and Event name, saved in variables $3, $4, $5, and $6.  Parenthesis to indicate a capture group.  Then Brackets for the start of Character Class group.  This character class has a space and the special \w value.  \w is any letter, upper and lower case A-Z and a-z, as well as the digits 0-9, and the underscore.  The asterisk will match 0 or more of those, but is modified by the Question Mark, which has a different meaning here.  Normally, the asterisk is greedy, which means it will match as much as possible.  The question mark means it will be lazy and match less.  This is here so that leading and trailing spaces aren't match and will instead by matched by the *- * sections on either side.

At this point I notice a minor mistake in my code that would allow trailing spaces to possible be at the end of the Event value if there are more than one between the Event and the Increasing number.  I'll list a fix below.

*\d+\..*$ -  (Corrected) The rest of the filename which is ignored.  I can give details on this if you really want, but it basically drops spaces, the Increasing number, and the extension of the file.

That completes the Search part of the RegEx.

/$1##$2##$3##$4##$5##$6/} - The Replace part of the RegEx.  The 6 values captured are listed, each separated by two hashtags, followed by the closing brace to indicated the end of the advanced formatting. 

Now back to the -Sep "##" part.  After all that advanced formatting, Filename will now look like this 2016##January##United States##NC##Raleigh##Zoo visit.  The -sep option is used to "Specify separator string for items in list-type tags".  In this case I'm using two hashtags as the separator and this will indicate where to separate the new "Filename" into individual keywords to be added.

To fix the minor mistakes I made, the full command would be better as this:
exiftool -Sep "##" "-keywords+<${Filename;s/(\d{4})-\d\d *\(([^)]*)\) *- *\d\d? *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*?) *- *([ \w]*?) *\d+\..*$/$1##$2##$3##$4##$5##$6/}" FileOrDir

The regex substitution that exiftool uses in the advanced formatting is complex and can be hard to get how it works, but it can be worth it to learn, especially if you get into regular programming.

But as I said, if you're using exiftool to do the rename into the above format, it would be much easier to copy the keywords from the original tags than to parse out the filename.
* 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).

thimplicity

Thanks again for all your help and the efforts for the explanations. I will try to sort out the confusion regarding the hyphens.

The naming convention is currently a plan, that I want to execute with the Advanced Renamer tool based on different sources:

  • Dates source: EXIF information based on created date
  • Location source: EXIF information based on GPS (if available)
  • Event source: information based on the folders above the files
  • Number: number calculated by the tool

As this is not executed yet, I can still replace the hyphens with something else.

Keywords seems to be the correct place, as I use OneDrive for managing and storing my photos and the keywords are shown in the correct way and I can search for it etc.

thimplicity

I have another question. I use an iPhone as my camera and automatically upload the pictures to a folder in onedrive. According to the renamer tool some of the photos do not have a created date, in the EXIF information, but the file name starts with the date information. Is there any chance of bringing this into the created date as well?

Example of the file name: 20171111_155538907_iOS.jpg

StarGeek

Quote from: thimplicity on November 12, 2017, 07:57:10 PM
Thanks again for all your help and the efforts for the explanations. I will try to sort out the confusion regarding the hyphens.

I think I accounted well for the hyphens in the command I listed and looking around on the web, hyphenated place names are not very common.  That only would leave the question of events.

QuoteThe naming convention is currently a plan, that I want to execute with the Advanced Renamer tool based on different sources:

  • Dates source: EXIF information based on created date
  • Location source: EXIF information based on GPS (if available)
  • Event source: information based on the folders above the files
  • Number: number calculated by the tool

Some of this is easier to copy directly with exiftool rather than parsing after the rename.  With some prep, maybe completely with exiftool.

For example, adding the year and month to keywords could be done with:
exiftool -d "%Y-%B" -sep "##" "-keyword+<${DateTimeOriginal;s/(\d{4})-(.*)/$1##$2/} FileOrDir

The Event name can also be parsed out of the Directory tag more easily depending upon the directory structure.

The location data is harder.  Exiftool can't currently do reverse geocoding to lookup the location data, but another tool can.  Geosetter is very powerful for taking the GPS coordinates from a file and filling out most relevant location data, as well as other metadata. Taking the extra step of embedding the location metadata first would mean it is always available.

Hmm... the only problem would be the State Abbreviation part.  The metadata tags store State as the full name.  But that could be easily worked around.  I think that's going to be my new side project.
* 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).

StarGeek

Quote from: thimplicity on November 12, 2017, 09:42:06 PM
I have another question. I use an iPhone as my camera and automatically upload the pictures to a folder in onedrive. According to the renamer tool some of the photos do not have a created date, in the EXIF information, but the file name starts with the date information. Is there any chance of bringing this into the created date as well?

Example of the file name: 20171111_155538907_iOS.jpg

There's a couple things I would check about this.  First, run this command:
exiftool -s -alldates 20171111_155538907_iOS.jpg
Odds are it will return a tag that has the proper date, either DateTimeOriginal, CreateDate, or ModifyDate.  If the file contains one or more matching timestamps, then I would run:
exiftool "-Alldates<Alldates" FileOrDir
and that will fill out the missing data.  Or you can check the other date/time variables in Advanced Renamer (Datetime tags, Datetime created tags, Datetime modified tags) and use one of them.

If that doesn't work, as long as the numbers in the filename are in ISO order (Year month date hours minutes seconds), then you can directly copy filename into the timestamps:
exiftool "-Alldates<Filename" FileOrDir

Always test first.  Once you're sure the commands work, you can add -r to your command to recurse into subdirectories and -overwrite_original to prevent backup files from being created.
* 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).

thimplicity

Thanks again, I understood that I can apply your commands to single files or directories.

How can I make sure it is executed for all files within a directory and all files in the respective subfolders (without touching directory names)?

StarGeek

Quote from: StarGeek on November 12, 2017, 10:31:39 PM
Always test first.  Once you're sure the commands work, you can add -r to your command to recurse into subdirectories and -overwrite_original to prevent backup files from being created.
* 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).

thimplicity

Quote from: StarGeek on November 12, 2017, 10:31:39 PM
Quote from: thimplicity on November 12, 2017, 09:42:06 PM
I have another question. I use an iPhone as my camera and automatically upload the pictures to a folder in onedrive. According to the renamer tool some of the photos do not have a created date, in the EXIF information, but the file name starts with the date information. Is there any chance of bringing this into the created date as well?

Example of the file name: 20171111_155538907_iOS.jpg

There's a couple things I would check about this.  First, run this command:
exiftool -s -alldates 20171111_155538907_iOS.jpg
Odds are it will return a tag that has the proper date, either DateTimeOriginal, CreateDate, or ModifyDate.  If the file contains one or more matching timestamps, then I would run:
exiftool "-Alldates<Alldates" FileOrDir
and that will fill out the missing data.  Or you can check the other date/time variables in Advanced Renamer (Datetime tags, Datetime created tags, Datetime modified tags) and use one of them.

If that doesn't work, as long as the numbers in the filename are in ISO order (Year month date hours minutes seconds), then you can directly copy filename into the timestamps:
exiftool "-Alldates<Filename" FileOrDir

Always test first.  Once you're sure the commands work, you can add -r to your command to recurse into subdirectories and -overwrite_original to prevent backup files from being created.

Unfortunately, that did not work, as some of my photos (for whatever reason) do not have any dates. What would be the command to transform the date information at the beginning of the filename (it is there, but not in the meta-data ...) into the meta data? The file name structure is "20150617_130958582_iOS.JPG"

Thanks again!

StarGeek

Quote from: StarGeek on November 12, 2017, 10:31:39 PM
If that doesn't work, as long as the numbers in the filename are in ISO order (Year month date hours minutes seconds), then you can directly copy filename into the timestamps:
exiftool "-Alldates<Filename" FileOrDir
* 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).

thimplicity

#12
Hi,
I have solved the date issues - thanks again.

I have now followed your advice and worked with Geosetter on getting the location data into the respective places (country, country code, region, city and location). What would be the commend to transform this information into a tag, instead of working with the filename? I found out that Geosetter can do that for me

I would also like to add the parent folder as well as two further parent folder names' as tags.
How can I transform the year and the month into Tags as well, the year as numbers (2017), but the month as the word (January)?

Thanks you so much again for your help and a big sorry for changing my approach constantly :)

StarGeek

Quote from: thimplicity on November 20, 2017, 09:59:41 AMzI found out that Geosetter can do that for me

Yeah, Geosetter is great!  When I figured out how richly it filled out the metadata, it changed my workflow for the better and made me rethink many of my scripts.

QuoteI would also like to add the parent folder as well as two further parent folder names' as tags.

To add the last three parent directories, try this:
-sep "##" "-Keywords+<${directory;s/.*\/([^\/]+?)\/([^\/]+?)\/([^\/]+?$)/$1##$2##$3/}"

This will fail if there aren't at least three parents

QuoteHow can I transform the year and the month into Tags as well, the year as numbers (2017), but the month as the word (January)?

Is this separate from adding the year as above?  Did you want to pull the year/month from a date tag instead?  Or did you just want to add the month name pulled from the filename above?

QuoteThanks you so much again for your help and a big sorry for changing my approach constantly :)

The number of times I've changed my approach to something after learning some new about exiftool is scary.
* 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).

thimplicity

Quote from: StarGeek on November 20, 2017, 08:31:11 PM
Is this separate from adding the year as above?  Did you want to pull the year/month from a date tag instead?  Or did you just want to add the month name pulled from the filename above?

I would like to pull both year and month from the date taken date tag. I have not renamed the file at all yet  :)