Copy tags to jpg only if matching xmp file exists

Started by dhackney, July 13, 2011, 11:04:00 PM

Previous topic - Next topic

dhackney

Phil,

Thanks for a great tool and especially for the extraordinary support you provide the user community.

I've searched and read many posts, but am unable to find this. I know I saw it a few days ago in a post here, but can't find it again.

Challenge: process a tag copy only if a corresponding/matching source file exists

I am working in directories with a mixture of RAW file sets (matching CR2 and sidecar XMP and JPG files) and standalone JPG files.

Example:
photo123.CR2
photo123.XMP
photo123.JPG
photoAAA.JPG
photoAAB.JPG
photoAAC.JPG

I am copying a date/time tag from the sidecar xmp files to the matching CR2 and JPG files.

I'd like to skip the standalone JPG files that do not have matching / corresponding XMP files.

I'm using a windows batch file and an args file.

Current command: exiftool -tagsFromFile %%d/%%f.xmp -r -@ lr-datetime-equalize.args -ext JPG -ext jpg . -overwrite_original -v0

Full implementation:
http://www.hackneys.com/travel/docs/lr-datetime-equalize.args
http://www.hackneys.com/travel/docs/lr-datetime-equalize.bat

Platform:
Windows 7 / 64
Exiftool 8.6

Note: I am 99% certain I read in a post here that -ext ignores case, but I can't find that post and it isn't explicitly stated in the docs: -ext EXT    (-extension)         Process files with specified extension

Thanks!

Phil Harvey

#1
Quote from: dhackney on July 13, 2011, 11:04:00 PM
Challenge: process a tag copy only if a corresponding/matching source file exists

[...]

Current command: exiftool -tagsFromFile %%d/%%f.xmp -r -@ lr-datetime-equalize.args -ext JPG -ext jpg . -overwrite_original -v0

Doesn't this already do what you ask?  You will simply get an error and no tags will be copied if the corresponding XMP file doesn't exist.  But if you must key on the XMP files, you can use the -srcfile option:

exiftool ... -ext xmp -srcfile %d/%f.jpg .

QuoteNote: I am 99% certain I read in a post here that -ext ignores case, but I can't find that post and it isn't explicitly stated in the docs: -ext EXT    (-extension)         Process files with specified extension

Read down a bit further in the documentation to the detailed description:

    -ext EXT, --ext EXT (-extension)
        Process only files with (-ext) or without (--ext) a specified
        extension.  There may be multiple -ext and --ext options.  Exten-
        sions may begin with a leading '.', and case is not significant.
        For example:
   
            exiftool -ext .JPG DIR            # process only JPG files
            exiftool --ext crw --ext dng DIR  # process all but CRW and DNG
            exiftool --ext . DIR              # ignore if no extension
   
        Using this option has two main advantages over specifying "*.EXT"
        on the command line:  1) It applies to files in subdirectories
        when combined with the -r option.  2) The -ext option is
        case-insensitive, which is useful when processing files on case-
        sensitive filesystems.


- Phil

Edit: Removed left-over quoted text after signature
...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 ($).

dhackney



Phil,

Thank you for the quick reply. Greatly appreciated. Your support for exiftool is truely exemplary.

QuoteDoesn't this already do what you ask?  You will simply get an error and no tags will be copied if the corresponding XMP file doesn't exist. 

Yes, it does work. However, I am planning to post the batch and args files for others facing this same date/time sync challenge, so I was hoping to eliminate the error messages and processing by skipping the standalone JPGs on that processing pass.

QuoteBut if you must key on the XMP files, you can use the -srcfile option:

exiftool ... -ext xmp -srcfile %d/%f.jpg .

Thanks for pointing me in the right direction. I did a search for forum posts with "-srcfile" and need to confirm some things:
1. Is -srcfile always paired with the parameter that FOLLOWS it, in this case ( -srcfile %d/%f.jpg ) versus ( -ext xmp -srcfile ) ?
2. Does -srcfile support the -ext qualifier? I found a post where you stated you were considering adding support for it. Example: -srcfile -ext jpg
3. Does %d/%f.jpg handle .jpg and .JPG, meaning is %d/%f.jpg case insensitive?
4. Does -srcfile %d/%f.jpg handle the -r in the current command recursing into subdirectories?

QuoteRead down a bit further in the documentation to the detailed description:

    -ext EXT, --ext EXT (-extension)

Yes, thanks.

Sorry, it's always one search/click/scroll/post/thread more where the answer lies.  :)

Please believe me when I tell you that most of us invest hours and hours, if not days and days, reading the docs, forum(s) and web sites searching for the answer before we ask you directly here.

Thanks again for your efforts and support.

Doug

Phil Harvey

Hi Doug,

Quote from: dhackney on July 14, 2011, 10:02:09 AM
1. Is -srcfile always paired with the parameter that FOLLOWS it, in this case ( -srcfile %d/%f.jpg ) versus ( -ext xmp -srcfile ) ?

No.  -srcfile applies to all source files for the command (but not -tagsfromfile files of course).  Order is not important.

Quote2. Does -srcfile support the -ext qualifier? I found a post where you stated you were considering adding support for it. Example: -srcfile -ext jpg

A reference to the post would be useful.  I don't have any idea what I could have been thinking.  The -ext option filters the file names before being modified by -srcfile.

Quote3. Does %d/%f.jpg handle .jpg and .JPG, meaning is %d/%f.jpg case insensitive?

No.  The file names you enter are case sensitive, which is unfortunate in this case, and may be a deal-breaker for you.

Quote4. Does -srcfile %d/%f.jpg handle the -r in the current command recursing into subdirectories?

Yes, that is why you use %d in the path to the actual file to be processed (if it is in the same directory as the file named on the command line).  All -srcfile does is re-map the name of the file to be processed from the file specified on the command line.  This can be very confusing of course, and I get confused myself when using this feature.

QuotePlease believe me when I tell you that most of us invest hours and hours, if not days and days, reading the docs, forum(s) and web sites searching for the answer before we ask you directly here.

Yes, I understand that the documentation is rather cumbersome.

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

dhackney

#4
Phil,

Thanks for the quick reply.


Quote from: Phil Harvey on July 14, 2011, 11:52:14 AM

Quote from: dhackney on July 14, 2011, 10:02:09 AM
1. Is -srcfile always paired with the parameter that FOLLOWS it, in this case ( -srcfile %d/%f.jpg ) versus ( -ext xmp -srcfile ) ?

No.  -srcfile applies to all source files for the command (but not -tagsfromfile files of course).  Order is not important.

Sorry for not being more clear on this question.

What I was attempting to ask was, "Is the source file to be matched against specified before or after the -srcfile parameter?"

In a broader sense, I am attempting to understand how -srcfile works so that I can successfully apply it to other situations for multiple file processing operations that require matching file pairs.

In your example command line you have "-ext xmp -srcfile %d/%f.jpg"

In my challenge, I am attempting to accomplish: 
"If an .xmp file exists that matches the filename of a .jpg file, then perform the tag copy from the .xmp file to the .jpg file"

JPG driven Logic example:
1. Scan directory for .jpg files
2. 123.jpg file exists
3. Is there a matching 123.xmp file?
4. If yes, perform tag copy
5. If no, skip and check the next .jpg file
6. If no more .jpg files, end

XMP driven logic example:
1. Scan directory for .xmp files
2. 123.xmp file exists
3. Is there a matching 123.jpg file?
4. If yes, perform tag copy
5. If no, skip (and optionally report error) and check the next .xmp file
6. If no more .xmp files, end


In your command line syntax for the -srcfile command:
a. Which logic model is used, jpg driven or xmp driven
b. Which of the three elements is doing which part of that logic (-ext xmp -srcfile %d/%f.jpg)?
(I understand the tag copy actions are not part of your command segment using these three elements)




Quote2. Does -srcfile support the -ext qualifier? I found a post where you stated you were considering adding support for it. Example: -srcfile -ext jpg

Quote from: Phil Harvey on July 14, 2011, 11:52:14 AM
A reference to the post would be useful.  I don't have any idea what I could have been thinking. 

The post is here: https://exiftool.org/forum/index.php/topic,1746.msg7603.html#msg7603

It relates to handling the -ext and case insensitivity.


QuotePlease believe me when I tell you that most of us invest hours and hours, if not days and days, reading the docs, forum(s) and web sites searching for the answer before we ask you directly here.

Quote from: Phil Harvey on July 14, 2011, 11:52:14 AM
Yes, I understand that the documentation is rather cumbersome.

I would say an embarrassment of riches rather than cumbersome.  :)

I will weigh in on the "exiftool for dummies" thread on some thoughts in that area.

Thanks,
Doug


[edit: add xmp driven logic ]

Phil Harvey

#5
Quote from: dhackney on July 14, 2011, 12:37:36 PM
Sorry for not being more clear on this question.

What I was attempting to ask was, "Is the source file to be matched against specified before or after the -srcfile parameter?"

Hmmm.  This is just about as clear as before, but apparently I wasn't clear in my answer:  The -srcfile FMT option may appear anywhere on the command line, and it re-maps the names of all processed files.  For example, the command:

exiftool a.xmp b.xmp -srcfile %f.jpg c.xmp d.jpg -ext xmp ...

will select files a.xmp, b.xmp and c.xmp, then remap their names to a.jpg, b.jpg and c.jpg.  The result is the same as this command:

exiftool a.jpg b.jpg c.jpg ...

(except that %e in any FMT arguments will be "xmp" in the first command, but "jpg" in the second)

Quote
Logic example:
1. Scan directory for .jpg files
2. 123.jpg file exists
3. Is there a matching 123.xmp file?
4. If yes, perform tag copy
5. If no, skip and check the next .jpg file
6. If no more .jpg files, end

With the -srcfile option, the logic is:

1. Scan the directory for .xmp files [the -ext xmp option]
2. 123.xmp file exists
3. Copy tags from 123.xmp to 123.jpg (will give error if 123.jpg doesn't exist)
     [note the processed file is 123.jpg, as set by the -srcfile option]
4. Skip to next .xmp file
5. If no more .xmp files, end

QuoteThe post is here: https://exiftool.org/forum/index.php/topic,1746.msg7603.html#msg7603

It relates to handling the -ext and case insensitivity.

Thanks for the URL.  This idea would not provide a case-insensitive solution unless this feature was specifically added.  With the -w EXT option, the specified extension is case-sensitive for example.  I think you are confused because of the  EXT argument to the  -w option.  We were not discussing the -ext option here.

QuoteI will weigh in on the "exiftool for dummies" thread on some thoughts in that area.

Great.  With enough input there I may eventually get motivated enough to tackle this job.

- Phil

Edit: realized a difference between the two commands in my example
Edit2: Indicate which option does what in the algorithm

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

dhackney

Phil,

Thanks for the explanation on the logic. That is very helpful.

Is there any way to handle upper and lower case JPG file extensions using the -srcfile option? Can I set up any type of logic test on the case of the filename extension and then choose the output case (upper or lower)?

This command line works for my workflow, but it won't work for anybody who has upper case .JPG extensions on their sidecar JPG files. Well, technically it will work, but it will change all of their sidecar JPG file extensions to lower case. That may not be a happy day for them.


For those who will find this thread on a search, my final command line is as follows. Note that I am driving this via a Windows batch file so I needed to use %% instead of a single %.

exiftool -tagsFromFile %%d/%%f.xmp -r -@ lr-datetime-equalize.args -ext xmp -srcfile %%d/%%f.jpg . -overwrite_original -v0>>lr-datetime-equalize.txt
                                                                                                 |
Note for Newbies: The . prior to -overwrite_original denotes the current Windows directory-------^ 

When you see the word DIR in the samples in the documentation and on the forum, you need to replace DIR with your absolute, specific directory path, such as c:\photos\work or a relative directory path such as ..\ or . as used in this command line.

thanks,
Doug


Phil Harvey

Quote from: dhackney on July 14, 2011, 02:09:31 PM
Is there any way to handle upper and lower case JPG file extensions using the -srcfile option? Can I set up any type of logic test on the case of the filename extension and then choose the output case (upper or lower)?

This could be done with some scripting, but I can't think of a way to do it with exiftool directly.

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

Phil Harvey

Quote from: Phil Harvey on July 15, 2011, 07:16:16 AM
Quote from: dhackney on July 14, 2011, 02:09:31 PM
Is there any way to handle upper and lower case JPG file extensions using the -srcfile option? Can I set up any type of logic test on the case of the filename extension and then choose the output case (upper or lower)?

This could be done with some scripting, but I can't think of a way to do it with exiftool directly.

I just thought of a way, but it is very tricky, and not very pretty:

exiftool -ext xmp -srcfile %d%f.jpg -if '$filename=~s/\.xmp$$/.jpg/i and -e "$directory/$filename"' ...

Here I manually substitute ".xmp" in the filename with ".jpg" (lower case, although ".xmp" is case-insensitive), then test with the perl -e operator to see if the file exists.   On case-sensitive filesystems, this would be followed by 2nd command to process upper-case .JPG files:

exiftool -ext xmp -srcfile %d%f.JPG -if '$filename=~s/\.xmp$$/.JPG/i and -e "$directory/$filename"' ...

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