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