how to copy string from one tag to another + modify

Started by remington_moll, November 30, 2016, 04:35:38 PM

Previous topic - Next topic

remington_moll

Hello,

I am trying to replace the value of one tag with the string of another tag plus a character string.

Specifically, I would like to grab the string in the Subject tag and paste it into the Hierarchical Subject tag preceded by the character string "SPECIES|"

E.g.

If Subject value is DEER, I would like to make the Hierarchical Subject value SPECIES|DEER

Pretty straightforward, but I am new to exiftool and could not find a relevant solution after a bit of searching on the forum or FAQ.

Thanks,

Remington

Phil Harvey

Hi Remington,

There are a few ways to do this.  Here is one:

exiftool -api filter="$_=qq(SPECIES|$_)" "-hierarchicalsubject<subject" DIR

This will replace any existing HierarchicalSubject tags.  Use "+<" instead of "<" if you want to add to existing HierarchicalSubject entries.

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

remington_moll

Hi Phil,

Thank you for such a prompt and helpful reply!

Unfortunately, the line of code you provided did not achieve what I was hoping for, but I think perhaps it is my fault for not being clear enough.

I have folders of images with various Subject tags, one of which is DEER. For some images, the Subject = DEER and HierarchicalSubject = SPECIES|DEER. This is the format I need for all, but modified accoridng to Subject (eg., HierarchicalSubject = SPECIES|DEER when Subject = DEER, HierarchicalSubject = SPECIES|FOX when Subject = FOX, etc). But some images have Subject = DEER and HierarchicalSubject = DEER. So what I am hoping to do is loop over all pictures and make the HierarchicalSubject = SPECIES|(Value of Subject).

For example, I used this line of code to achieve the desired results:

exiftool -if "$Subject eq 'DEER'" -hierarchicalsubject="SPECIES|DEER" DIR

That worked because an image with Subject = DEER and HierarchicalSubject = DEER is changed to Subject = DEER and HierarchicalSubject = SPECIES|DEER, while an image that is already tagged properly (e.g., Subject = DEER, HierarchicalSubject = SPECIES|DEER) gets "changed" to the same.

Problem is I would need to run that for each of the different subjects, of which there are quite a few, over many tens of thousands of images, so I am trying to find a silver bullet.

I hope this is a more clear articulation and again my sincere thanks for any help.

Remington

Hayo Baan

Strange, to me it looked like it would achieve exactly what you wanted, but I just tested it myself and apparently it doesn't apply the filter Phil specified...

Anyway, this command should work for you:
exiftool "-hierarchicalsubject<${subject; $_=qq(SPECIES|$_)}" DIRSorFILES
Hayo Baan – Photography
Web: www.hayobaan.nl

remington_moll

Thank you Hayo Baan!

Worked brilliantly. Much appreciated.

Phil Harvey

Odd.  It works for me:  (note that I needed to use single quotes around one argument because I am on a Mac)

> exiftool -subject a.jpg
Subject                         : x, y
> exiftool -api filter='$_=qq(SPECIES|$_)' "-hierarchicalsubject<subject" a.jpg
    1 image files updated
> exiftool -hierarchicalsubject a.jpg
Hierarchical Subject            : SPECIES|x, SPECIES|y


I did it this way so it would handle multiple items properly.  Hayo's command will convert multiple items into a single item and add "SPECIES|" to the start of the combined item, which would be wrong.  But if there is only ever one Subject item then it will work.

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

remington_moll

Strange. Yes, you are correct that Hayo's command does convert multiple items into a single item. I did not realize that, thank you for pointing out!

However, I tried again and still cannot get the line of code that works for you to work for me. When I run it just as you have it (except for double quotes since I'm on a PC), it returns the hierarchical subject as: x,y

I can use a work around via the -if statement to fix the issue left over from Hayo's command, but might there be anything else to try?

Thanks,

Remington

Phil Harvey

An idea:  Are you by chance using an old version of ExifTool?  The Filter option was added in version 10.05.

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

Hayo Baan

#8
Quote from: Phil Harvey on December 01, 2016, 09:14:09 PM
An idea:  Are you by chance using an old version of ExifTool?  The Filter option was added in version 10.05

I'm on 10.36 and it didn't work for me neither... Hence my work-around.

Weird, just tried again and this time it worked as it should...
Hayo Baan – Photography
Web: www.hayobaan.nl

remington_moll

Looks like I'm using 10.1.2. Will try again later today and see if Hayo's luck has transferred to me. Thanks.

remington_moll

Update: Just ran Phil's code with the latest version and worked perfectly.

Many thanks!