Copy file based on text tag

Started by iratemonkey, October 14, 2015, 05:15:40 AM

Previous topic - Next topic

iratemonkey

Hi,

I am trying to copy photos containing a certain text tag (Cape Porcupine) from one directory to another. I adapted the advice given in this post https://exiftool.org/forum/index.php/topic,3531.msg16047.html#msg16047.

I used this command:

exiftool test -if '$XMP:Subject = "Cape Porcupine"' -o '/Users/username/capeporcupine'

to try and copy all photos with the tag Cape Porcupine from the test directory (NB:I set my working directory to the one that contains the test folder) to the capeporcupine directory. However using this command moved all the photos regardless of tag rather than only the ones tagged Cape Porcupine under the tag XMP:Subject.

I think the error could be with the equals sign perhaps??

Any help would be appreciated.

Thank you!

Hayo Baan

Try it with a double equals sign ==. You are now assigning, not comparing  :)
Hayo Baan – Photography
Web: www.hayobaan.nl

Phil Harvey

Hayo has the right idea, but the double equals (==) is a numerical equality test.  Use "eq" instead for a string comparison.

Also, if "capeporcupine" is a directory then it is safest to use a trailing "/".  ie) -o '/Users/username/capeporcupine/'  Otherwise if the directory doesn't exist or if you spelled it wrong or something then the first file would be renamed to "capeporcupine" and the rest of the files would give "already exists" errors.  Adding the trailing "/" makes this unambiguously a directory name.

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

iratemonkey

Brilliant!! It worked perfectly. Thanks so much.

Hayo Baan

Quote from: Phil Harvey on October 14, 2015, 07:23:33 AM
Hayo has the right idea, but the double equals (==) is a numerical equality test.  Use "eq" instead for a string comparison.

Oops, sorry about the mix up, I'm on holiday now and have been programming too many other languages lately to remember the correct usage of eq. Sorry  ::)
Hayo Baan – Photography
Web: www.hayobaan.nl

iratemonkey

I encountered a new challenge which I wondered if you could help with...when I moved the images from a directory with multiple sub-folders to one directory, images with the same name were found and errors came up. I want to rename all the images (or just the ones with the same name) as they move into the new directory to fix this problem. I think I will need to use the command %C or %%-c but I don't know where to place this within my script. I tried several places and it didnt work. Which is the right command and where do I place it in the above script line? Thanks very much.

Phil Harvey

The command could look something like this:

exiftool -if '$XMP:Subject eq "Cape Porcupine"' -o '/Users/username/capeporcupine/%f%-c.%e' DIR

Except that this command copies the images instead of moving them as you mentioned.

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

iratemonkey

Great, it worked. Thanks a lot Phil! I really appreciate all your help and am very grateful to you for developing and maintaining such a useful tool. A donation to cover a couple of beers is on its way to you!

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

iratemonkey

One more question. I am now trying to get this running on a windows computer. I have replaced the single quote marks with double quote marks, but the script seems to need some additional tweaking to get it to run. Here is what I tried:

exiftool -r -if "$XMP:Subject eq "Leopard"" -o "E:\leopard_exif\150626-150702\%f%-c.%e" "E:\leopard_exif\Leopard_output\"

What other changes do I need to make?

Thanks a lot!

Sam

Phil Harvey

Hi Sam,

Try this:

exiftool -r -if "$XMP:Subject eq 'Leopard'" -o "E:\leopard_exif\150626-150702\%f%-c.%e" "E:\leopard_exif\Leopard_output\"

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

iratemonkey

Thanks Phil, but this didn't work either. Nothing was copied to the output folder despite some photos having that text in the appropriate metadata field (plus the previous code worked on the same folders under os x). Do you have any other ideas?

Thanks a lot.

Phil Harvey

Could you send me an image that you think should have been moved?  My email is philharvey66 at gmail.com

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

iratemonkey

Thanks a lot Phil, I am emailing you a photograph now.

Sam

Phil Harvey

Hi Sam,

I got your image, thanks.

The XMP:Subject is exactly equal to "Leopard", so your command should work.  I verified this on a PC with the following command (after renaming the image to "a.jpg" in the current directory):

exiftool -o tmp -if "$xmp:subject eq 'Leopard'" a.jpg

which gave a "1 image files copied" message.

What messages do you get when you run your command?  Perhaps there is something wrong with the way your are specifying the directory name.

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