Can you rename a raw file and create tag in XMP file at the same time?

Started by Ad_Astra, February 06, 2025, 06:43:00 AM

Previous topic - Next topic

Ad_Astra

This command line works as expected and renames both XMP and NEF files.

exiftool -tagsfromfile %d%f.xmp "-filename<${PreservedFileName}.%le" -ext nef -ext xmp
This command line only renames XMP files, NEF files are not renamed and give error "Warning: Error opening file"

exiftool -tagsfromfile %d%f.xmp -fileorder -filename "-filename<${PreservedFileName}.%le" -ext nef -ext xmp
The fileorder looks to be important so wondered if there is a -fileorder option I should use to make the command line more robust.

StarGeek

Ah, yes. Remove the -fileorder option when renaming from the XMP files (e.g. -TagsFromFile %d%f.xmp), that way the NEF files are renamed before the XMP files.

When doing a rename based upon the NEF files (e.g. -TagsFromFile %d%f.nef) then -fileorder -filename is necessary to rename the XMP files before the NEF files.

"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Phil Harvey

... or better to just remove the dash from -filename to order in the opposite direction (ie. -fileorder filename) so that we don't leave it up to the whims of the filesystem to order things properly.

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

StarGeek

Quote from: Phil Harvey on February 07, 2025, 02:10:09 PM... or better to just remove the dash from -filename to order in the opposite direction (ie. -fileorder filename) so that we don't leave it up to the whims of the filesystem to order things properly.

As with my DrivePool, which returns the file list in an order which depends upon the actual underlying drive the file is on. For example, the NEF could be on one drive and the XMP on another, but they are on the same "Drive" in the drive pool.

And in this case -fileorder4 would be best to avoid the multiple passes across the files. Thinking about it, I probably should add that as a default option in my config file.

Edit: Actually, setting it in the config file means that fileorder is processed first, which would interfere if I wanted to sort a different way such as -fileorder -DateTimeOriginal.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Ad_Astra

Quote from: Phil Harvey on February 07, 2025, 02:10:09 PM... or better to just remove the dash from -filename to order in the opposite direction (ie. -fileorder filename) so that we don't leave it up to the whims of the filesystem to order things properly.

- Phil

Thanks StarGeek for the explanation on why to use fileorder, I have added Phil's suggestion to remove the - to make sure order is in opposite direction.

Tomorrow's task is to look at MOV movie files and JPEG as I use both of these filetypes with my Nikon Camera.

A quick test in Adobe Bridge renaming both a MOV file and JPEG file created a Preserved Filename tag entry, as there is no XMP file for MOV and JPEG files I assume it is writing to some data area in the MOV / JPEG file directly?

So do I just need to apply the same logic without the reading / writing to the XMP file?

Ad_Astra

Quote from: StarGeek on February 07, 2025, 02:26:15 PMAnd in this case -fileorder4 would be best to avoid the multiple passes across the files. Thinking about it, I probably should add that as a default option in my config file.


I saw the reference to -fileorder4 in the documentation but did not fully understand it, should I use

-fileorder4 -fileorder filename
For faster processing?

StarGeek

Not quite. You would use
-fileorder4 filename

By default, the -fileorder reads all the files once in order to collect the data it needs in order to sort the files, then again when it processes the commands. Using -fileorder4 means that it will only read the file system tags, i.e. it will not open up the file to read the contents. This is significantly faster.

-fileorder4 will only work for file system data. If you wanted to sort on a tag such as DateTimeOriginal, then the command would take longer.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Ad_Astra

Something of a corner case I wondered how to handle voice memo files. A quick test in Adobe Bridge I selected just a nef file that had a wav voice memo as well, but only selected the nef file, did a file rename and Adobe Brdige updated the wav file name as well, I was impressed so thought my script needed to do the same and handle any voice memo files.

I created a test folder with 4 nef files, two of which had a voice memo wav file and a jpeg file that also had a voice memo file.

I tried this code

exiftool -P -d %Y%m%d_%H%M%S-%-3f -tagsfromfile %d%f.NEF -fileorder4 -filename "-filename<myname_${subsecdatetimeoriginal;}_%f.%le" -ext nef -ext xmp -ext wav
This works for the NEF files with voice memos but it also tries to find a nef file with the name of jpeg file and as expected gives an error message. i.e the "-ext wav" picks up all wav files not just those matched to nef files.

Any suggestions how to match the wav voice memo files to the correct photo file format nef or jpg please? i.e. only update the wav filename if it has the same base filename as the nef file? Hope this makes sense it is hard to describe.



Phil Harvey

I'm not clear on the file associations.  You've just added WAV and JPG files to the mix.  Do they all have XMP sidecars?  The solution is probably to  add multiple -tagsFromFile options (setting filename from each) and just ignore the warning messages for the files that don't exist.

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

StarGeek

You can add
-api NoWarning="Error opening file"
to suppress the warnings for files that don't exist.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Ad_Astra

Many thanks for the advice, getting closer to a working solution.

I ran my script on the camera files from a recent holiday and it worked fine for most of the time. I had a few voice memo files which I had not thought about and some jpeg files as well as Nikon only does precapture in jpeg format. As Adobe updates all associated files when reanming I thought I should do the same in my script. I have a simple test folder with the following files:

Z81_5961.NEF
Z81_5961.wav
Z81_5962.NEF
Z81_5962.wav
Z81_2090.jpg
Z81_2090.wav

So what I am trying to do is for each photo file format NEF or JPEG rename the photo file and all its assoicated files. So for a NEF file the associated files could be XMP and WAV and for JPEG the associated files could be WAV.

I tried to create some "if test" with base filename test but could not get a working solution. My attempt at multiple -tagsfromfile went very wrong, only the JPEG and its associated wav was renamed, the nef files were not renamed but duplicated with _original also had error messages: Error: Can't currently write RIFF WAVE files - xxxxxxx.wav.

The code I ran was a two pass:

exiftool -P -overwrite_original -wm cg "-XMP:PreservedFileName<Basename" -srcfile %d%f.xmp -ext nef .

exiftool -api NoWarning="Error opening file" -P -d %Y%m%d_%H%M%S-%-3f -tagsfromfile %d%f.NEF -tagsfromfile %d%f.JPG -fileorder4 -filename "-filename<myname_${subsecdatetimeoriginal;}_%f.%le" -ext nef -ext xmp -ext wav -ext jpg .



The first command created the XMP file for the NEF files with just the preserved filename tag as expected. The second command created a second copy of the XMP with lots of tags filled in and renamed the first XMP as xxx.xmp_orignal.

The second command did not rename the nef, but created a copy xxx.nef_original. The wav file associated with the NEF files was not renamed, I think these are related to the error writing message as above.

However the jpeg and its assocated wav file were renamed.


I will keep testing, if you have any suggestions how to fix the second command I would be very grateful.



StarGeek

Quote from: Ad_Astra on February 09, 2025, 10:02:21 AMThe second command created a second copy of the XMP with lots of tags filled in and renamed the first XMP as xxx.xmp_orignal.

You forgot the -overwrite_original option.

Edit: See below
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Ad_Astra

I left -overwrite_original off the second command as it is working on the original raw file or jpeg image, my example code with two -tagsfromfile is overwriting the nef and creating a copy as xxx.nef_original. Don't like the idea of overwriting the raw file, I am OK with overwriting an XMP file. Not sure why having two -tagsfromfile commands is doing this unless it is forcing two passes so the second pass updates the original raw file.

Maybe I need to run this as two separate commands? I haven't been able to find a way using an if statement to match the wav file with its parent image file.

StarGeek

Your command should be this:
exiftool -api NoWarning="Error opening file" -P -d %Y%m%d_%H%M%S-%-3f -tagsfromfile %d%f.NEF "-filename<myname_${subsecdatetimeoriginal;}_%f.%le" -tagsfromfile %d%f.JPG "-filename<myname_${subsecdatetimeoriginal;}_%f.%le" -fileorder4 -filename  -ext nef -ext xmp -ext wav -ext jpg .

You need to include the rename after each -TagsFromFile option.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Ad_Astra

#29
Many thanks StarGeek that works great, many thanks to Phil and StarGeek for helping me on my journey to understand Exiftool better.

I only have two small things left to do now:

1) add the PreservedFileName to the JPEG files. As there is no XMP file for JPEGs do I just update the PreservedFileName tag directly in the JPEG file? I.e. is it safe and does not cause the picture data to be recompressed and lose of image quality?
2) add a copy number in case of duplicates. As I include original file name I don't think it should happen but to be safe does this "-filename<myname_${subsecdatetimeoriginal;}_%f%+nc.%le" add -1 -2 etc only if there are duplicates?