News:

2023-03-15 Major improvements to the new Geolocation feature

Main Menu

Changing format of metadata from #### NAME to NAME-####

Started by phillyphotographer, June 06, 2017, 10:57:10 PM

Previous topic - Next topic

phillyphotographer

Hi there - I'm a new user to ExifTool and need some help.

Suppose I have 5000 images with this metadata in the field LABEL.

0001 ELEPHANT
0002 ELEPHANT
0003 ELEPHANT
...

4999 ELEPHANT
5000 ELEPHANT

I wish to change it to this format:

ELEPHANT-0001
ELEPHANT-0002
ELEPHANT-0003
...
ELEPHANT-4999
ELEPHANT-5000

Does anyone know how to do this?

In sum, I want to do this for a metadata field, over and over:

##### NAME     —>     NAME-####


I can't seem to find anything that it this complicated in the ExifTool documentation or the Exiftool examples I searched for.

Any suggestions?

If it helps, here is how I would explain this in words: for each image, I wish ExifTool to read the metadata field, which is a string, then identify the number in that metadata, save that to memory, identify the word, then make a new string made by concactenating the name and the number with a hyphen in between, THEN save that new string to the metadata field.

Did I explain that correctly?

Alternative (might be a lot simpler?): have ExifTool reference an external file (plain text? CSV?), and write new metadata from that file (no idea if that is easier...I haven't seen much about ExifTool referencing external files other than images.

StarGeek

Try
exiftool "-LABEL<${LABEL;s/(\d{4}) (.*)/$2-$1/}" FileOrDir

If not on Windows, change double quotes to single quotes.  Replace FileOrDir with the directory or filenames to process.

* 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).

phillyphotographer

Fantastic! Thanks for the quick reply. I will give it a try.

phillyphotographer

For future reference, this was super helpful (posted from stack exchange):

The basic command would be
exiftool "-LABEL<${LABEL;s/(\d{4}) (.*)/$2-$1/}" <FileOrDir>

You basically want to copy a tag into the same tag, with some modifications. The option to copy a tag is the less than (or greater than) symbol < or >. A common mistake is to use the equal sign = which is used to assign a static value to a tag.

To do the modification to the tag, it takes the Advance Formatting option, which is actually some in-line perl code. In this example, the tag is treated as a perl string and a regex substitution is used. It matches and captures the first four digits (\d{4}), matches the space (but doesn't capture it), then matches and captures the rest of the tag (.*). The two captures are assigned to the variables $1 and $2, respectively. In the replace half of the substitution $2-$1, the two captures are reversed with the hyphen between them.

To take full advantage of the advance formatting, some basic perl and regex knowledge is helpful.

Once you are sure of the command, you can add -overwrite_original to suppress the generation of backup files and -r to recurse into subdirectories.


phillyphotographer

This worked beautifully. The explanation was very, very helpful.
;D ;D ;D

StarGeek

Yeah, I tend to explain each part more in depth at StackExchange.  My small attempt to make them more familiar with exiftool.  They tend to want to silly things like loop through and run exiftool once for every file.
* 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).

phillyphotographer

I would have said more on SE, but I found out that SE guidelines encourage against posting "thanks" as a reply!

:-)