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

What I would like to do is use the ExifTool to rename my camera raw files to "myname_YYYYMMDD_HHMMSS-MMM_originalFilename" and at the same time create an XMP file to record the original file name in the Adobe XMP tag PreservedFileName.

I have made some progress:

exiftool.exe -fileOrder DateTimeOriginal -d %Y%m%d_%H%M%S "-filename<myname_${createdate;}-${EXIF:SubSecTimeOriginal;$_ .= '0' x (3-length)}_%f.%e" "-XMP:PreservedFileName<${Basename;}" -srcfile %d%f.xmp -ext nef .
I am stuck with how to create the XMP filename, the above fails with Error opening file - name of XMP file. In the above code -srcfile tries to create the XMP file name from the original filename which maybe the issue but I am stuck how to correct this. Any help appreciated.

Using ExifTool on Windows 10.

Next I would like to modify the code to only create the PreservedFileName tag if it is blank, but need to fix the creating XMP file first.

StarGeek

I'm pretty sure that this will have to be done in two separate commands, as creating a sidecar file and renaming are two separate operations.

One minor issue is that you are mixing the DateTimeOriginal tags and CreateDate tags. You are sorting on DateTimeOriginal, but renaming partially based upon CreateDate and partially on the SubSecTimeOriginal which matchesDateTimeOriginal, not CreateDate. The subsecond tag which matches CreateDate is SubSecTimeDigitized.

You should instead either use SubSecDateTimeOriginal or SubSecCreateDate, as these composite tag combine the main tag which has the date/time, the subsecond tag, and the offset time tag (timezone).

I would suggest replacing this
-fileOrder DateTimeOriginal -d %Y%m%d_%H%M%S "-filename<myname_${createdate;}-${EXIF:SubSecTimeOriginal;$_ .= '0' x (3-length)}_%f.%e"
with
-fileOrder SubSecDateTimeOriginal -d %Y%m%d_%H%M%S-%-3f "-filename<myname_${SubSecDateTimeOriginal;}_%f.%e"
"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

Quote from: StarGeek on February 06, 2025, 10:16:17 AMI'm pretty sure that this will have to be done in two separate commands

Correct.  The first command would create an XMP sidecar with the same name as the original file, and the second command would rename the original and the sidecar according to the SubSecDateTimeOriginal of the NEF file.

1. exiftool.exe "-XMP:PreservedFileName<Basename" -srcfile %d%f.xmp -ext nef .

2. exiftool.exe -d %Y%m%d_%H%M%S-%-3f -tagsfromfile %d%f.NEF -fileorder -filename "-filename<myname_${subsecdatetimeoriginal;}_%f.%e" -ext nef -ext xmp .

The -fileorder in the 2nd command makes sure the xmp gets moved first so the NEF exists when this is done.

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

Ad_Astra

Many thanks Phil and StarGeek, very useful and I am much closer now.

1) The example to create XMP file with PreservedFileName works fine, I have tried to modify it so if PreservedFileName already exists it does not update (just in case I run the command twice on a folder I want to protect the first original filename)

This is the code I tried which did not work, trying to if test that a XMP file does not exist or if it exists does not have PreservedFileName tag then either create XMP file with PreservedFileName tag or just update the tag in existing XMP file.

exiftool.exe -if "not $PreservedFileName" "-XMP:PreservedFileName<Basename" -srcfile %d%f.xmp -ext XMP .
2) The second example I could not get it to rename the XMP file created with first command, only the NEF raw file was renamed.The xmp file remained as originalname.xmp. I got warning for each file


QuoteWarning: Error opening file - ./Z81_5961.NEF

Any suggestions on how to conditionally add the PreservedFileName to the XMP file in line 1 and why line 2 fails with the XMP file rename please?

Phil Harvey

I had edited my post to add a -fileorder option to the 2nd command.  Did you see this?

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

Ad_Astra

Thanks Phil, just retried your second command and it now renames both the XMP and NEF files. That's great.

Any suggestions on conditionally updating the PreservedFileName tag please?

Phil Harvey

I'll have to come back to that.  It will be tricky and it's lunch time for me now. :P

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

Ad_Astra

No rush, thanks for the help with the file naming. I have enough to start building the Windows cmd file.

StarGeek

Quote from: Ad_Astra on February 06, 2025, 11:46:54 AMAny suggestions on conditionally updating the PreservedFileName tag please?

Add the -wm cg (-writeMode cg) option
exiftool.exe -wm cg "-XMP:PreservedFileName<Basename" -srcfile %d%f.xmp -ext nef .

This will not write existing tags, only create new tags.
"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 StarGeek, that does works it creteas an XMP file if none exisits or creates the PreservedFileName tag if XMP file exists with no PreservedFileName tag. The latter also creates a nnn.xmp-original file.

I tried the following

exiftool.exe"  -overwrite_original -wm cg "-XMP:PreservedFileName<Basename" -srcfile %d%f.xmp -ext nef .
Seems to work, is this recommended? I think I am OK to overwrite an XMP file.

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

Ad_Astra

Thanks for all the help with the above request. I tried to create the obvious follow up command to restore the filename back using the PreservedFileName tag but my effort fails.

exiftool.exe "-filename<${PreservedFileName;}.%e" -ext nefexiftool.exe "-filename<${-XMP:PreservedFileName;}.%e" -ext nef
Both fail with similar error message

QuoteTag 'PreservedFileName' not defined
QuoteTag '-XMP:PreservedFileName' not defined

Thought it would be an easy one, can you help spot my error please?




Phil Harvey

The tag is in the sidecar file:

exiftool.exe -tagsfromfile %d%f.xmp "-filename<${PreservedFileName}.%e" -ext nef DIR

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

Ad_Astra

Thanks Phil, I added -ext xmp to the command to rename the xmp files as well, which worked.

I then tried adding the option -fileorder -filename like in the original rename command above but this failed and only renamed the NEf files and not the XMP files. This there a -fileorder option I should use to make the command more error proof?

StarGeek

Please show us the exact command that you used so we can see where the error might be.
"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

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?


Phil Harvey

Quote from: Ad_Astra on February 09, 2025, 02:01:55 PM1) 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?

ExifTool doesn't change the image data.

Quote2) 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?

%-c will give you NAME.EXT and NAME-1.EXT if you have 2 files with the same name.  If you wanted NAME-1.EXT and NAME-2.EXT instead, things would be more difficult.

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

Ad_Astra

Thanks Phil, would %-nc be sufficient to create NAME-1.EXT and NAME-2.EXT?

Phil Harvey

%-nc will give NAME.EXT and NAME-2.EXT.

%-.nc will give NAME-1.EXT and NAME-2.EXT.

But %-.nc will also give NAME-1.EXT for a file that doesn't isn't a duplicate.  And you said you wanted only duplicate files to have the copy number.

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

Ad_Astra

Hi

I have been trying to add another tag to the initial XMP file.

Original code was
Quoteexiftool -P -overwrite_original -wm cg "-XMP:PreservedFileName<Basename" -srcfile %d%f.xmp -ext nef

I wish to update the XMP Title tag to the new name without extension, whilst the file has not been renamed yet with the command above I do know how I am going to rename it so I tried the following:

Quote-P -overwrite_original -wm cg "-XMP:PreservedFileName<Basename" -d %Y%m%d_%H%M%S-%-3f -tagsfromfile %d%f.NEF "-XMP-dc:Title<myname_${subsecdatetimeoriginal;}_%f" -srcfile %d%f.xmp -ext nef

This created a tag value with %f

Quote<dc:title>
   <rdf:Alt>
    <rdf:li xml:lang='x-default'>myname_20250121_094041-690_%f</rdf:li>
   </rdf:Alt>
  </dc:title>

I was please the date and time worked but puzzled why the original name part is just the text %f. Replacing %f with Basename did not work either. Any suggestions please what I am doing wrong?

edit updated code to remove Windows escape char.

StarGeek

The %f filename variable can only be used with specific commands. It is not a variable that can be copied into another tag. For that, you have to use either Basename or Filename
"-XMP-dc:Title<myname_${subsecdatetimeoriginal;}_$Filename"

Oh, I have a copy/paste for this that I forgot about

The percent tokens such as %d, %f, %e, etc, can be used in assigning a value to the Filename or Directory pseudo-tags (see Writing "FileName" and "Directory" tags) and in these options
-TAG<=DATFILE option
-TagsFromFile option
-w (-TextOut) option
-W (-TagOut) option
-o (-out) option
-srcfile 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

Phil Harvey

(to summarize: the %f filename format code may only be used in places where a file name is expected)

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

Ad_Astra

Thanks Phil and StarGeek, %f is the wrong thing to use and I missed the $ sign out when I tried Basename so this now works in my script:

"-XMP-dc:Title<myname_${subsecdatetimeoriginal;}_$Basename"