ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: iratemonkey on October 14, 2015, 05:15:40 AM

Title: Copy file based on text tag
Post by: iratemonkey on October 14, 2015, 05:15:40 AM
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!
Title: Re: Copy file based on text tag
Post by: Hayo Baan on October 14, 2015, 07:15:08 AM
Try it with a double equals sign ==. You are now assigning, not comparing  :)
Title: Re: Copy file based on text tag
Post by: 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.

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
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 14, 2015, 08:21:30 AM
Brilliant!! It worked perfectly. Thanks so much.
Title: Re: Copy file based on text tag
Post by: Hayo Baan on October 14, 2015, 05:34:02 PM
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  ::)
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 16, 2015, 08:57:13 AM
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.
Title: Re: Copy file based on text tag
Post by: Phil Harvey on October 16, 2015, 09:18:12 AM
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
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 23, 2015, 05:34:02 AM
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!
Title: Re: Copy file based on text tag
Post by: Phil Harvey on October 23, 2015, 07:25:16 AM
Thanks!
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 28, 2015, 04:56:05 AM
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
Title: Re: Copy file based on text tag
Post by: Phil Harvey on October 28, 2015, 07:14:07 AM
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
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 28, 2015, 09:22:21 AM
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.
Title: Re: Copy file based on text tag
Post by: Phil Harvey on October 28, 2015, 09:31:49 AM
Could you send me an image that you think should have been moved?  My email is philharvey66 at gmail.com

- Phil
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 28, 2015, 10:14:44 AM
Thanks a lot Phil, I am emailing you a photograph now.

Sam
Title: Re: Copy file based on text tag
Post by: Phil Harvey on October 28, 2015, 10:45:09 AM
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
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 28, 2015, 01:58:12 PM
Hi Phil,

When I run from a batch file:

exiftool -r -if "$XMP:Subject eq 'Leopard'" -o "Z:\leopard_exif\leopard_input\%f%-c.%e" "Z:\leopard_exif\leopard_output"

The command prompt appears, prints the above code, along with "Nothing to write".

"Z:\leopard_exif\leopard_input" contains the file I sent you.
Title: Re: Copy file based on text tag
Post by: Phil Harvey on October 28, 2015, 02:05:49 PM
Ah.  You didn't mention the batch file.

In a windows batch file, all percent characters must be doubled:

exiftool -r -if "$XMP:Subject eq 'Leopard'" -o "Z:\leopard_exif\leopard_input\%%f%%-c.%%e" "Z:\leopard_exif\leopard_output"

- Phil
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 28, 2015, 02:47:21 PM
Sorry, my bad. I just ran the code you suggest as a batch file and this time the error is:

1 directories scanned
0 image files read

Title: Re: Copy file based on text tag
Post by: Phil Harvey on October 28, 2015, 05:45:20 PM
OK, we're narrowing things down anyway.  What is the output of this command?:

dir "Z:\leopard_exif\leopard_output"

Oh, wait.  From the directory names it seems you may have the input and output directories reversed.  The output directory should go immediately after the -o (output) option.

- Phil
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 29, 2015, 03:04:18 AM
Thanks Phil! You are right. Somewhere in my trial and error I must have switched the order of the input and output directories. The following code just worked:

exiftool -r -if "$XMP:Subject eq 'Leopard'" -o "Z:\leopard_exif\leopard_output\%%f%%-c.%%e" "Z:\leopard_exif\leopard_input"

Thanks again so much! A bit more beer money is on its way to your account as a small thank you for your time and help.

Sam
Title: Re: Copy file based on text tag
Post by: Phil Harvey on October 29, 2015, 07:22:59 AM
Thanks!
Title: Re: Copy file based on text tag
Post by: iratemonkey on October 29, 2015, 07:34:17 AM
You're welcome. I also noticed that I was sometimes adding a \ to the end of the input directory path, which stopped it from working. So:

exiftool -r -if "$XMP:Subject eq 'Leopard'" -o "Z:\leopard_exif\leopard_output\%%f%%-c.%%e" "Z:\leopard_exif\leopard_input\"

did not work, while:

exiftool -r -if "$XMP:Subject eq 'Leopard'" -o "Z:\leopard_exif\leopard_output\%%f%%-c.%%e" "Z:\leopard_exif\leopard_input"

did work.
Title: Re: Copy file based on text tag
Post by: Phil Harvey on October 29, 2015, 07:38:49 AM
Right.  Another Windows feature.  Backslash before a quote is special.  This is explained by StarGeek in this post (https://exiftool.org/forum/index.php/topic,6655.msg33309.html#msg33309).

Presumably this wouldn't be a problem if you used forward slashes (which are perfectly acceptable to ExifTool).

- Phil