While organizing my pictures I typically use many operations of exiftool to adjust date/time, rename the file, bulk add GPS locations, add creator names etc. In this process, each JPG image is typically renamed and their tags processed multiple times.

In this process I want to keep the original filename (without file extension), just like the camera/phone has written it. When running a bulk operation over a large number of files, there may be some of them still with their original filenames and others may already be renamed and have their original filename already set in the preserved filename tag.


Is there an exiftool command that sets the XMP preserved filename tag only on the first time, i.e. if the preserved filename tag is empty and otherwise leaves it untouched?


1 Answer 1


Combining the "write only if not already existing" script snippet from this forum post with the "write preserved filename without file extension" expression from here, you get the wanted behavior:

exiftool "-XMP-xmpMM:PreservedFileName<${filename;s/\.[^.]*$//;$_ = undef if $self->GetValue('xmp:PreservedFileName')}" 'FILEorDIR'
  • 2
    \$\begingroup\$ If using exiftool v12.22 or higher, then using Basename will negate the need for the regex s/\.[^.]*$//; If the only edit is to add the PreservedFileName tag, then the command can be simplified to exiftool -wm cg "-PreservedFileName<Basename" /path/to/files/ \$\endgroup\$
    – StarGeek
    Commented May 4, 2023 at 15:55
  • 1
    \$\begingroup\$ @StarGeek: did not know the -wm cg option yet, thanks a lot for pointing this out - this comes in handy if I want to add tags (and only add them) to a lot of files at once. \$\endgroup\$ Commented May 4, 2023 at 19:58
  • \$\begingroup\$ And if I want to combine this with other options that should overwrite tags, it would still be something along the lines of exiftool "-PreservedFileName<${Basename;$_ = undef if $self->GetValue('PreservedFileName')}" /path/to/files/ right? \$\endgroup\$ Commented May 4, 2023 at 19:59
  • 1
    \$\begingroup\$ Yes, in that situation, you would use that command. You didn't mention what command line you where using. You would use double quotes as written when using Windows CMD. On Mac/Linux, you would swap the double/single quotes. Windows Powershell gets weird on complex exiftool command like this, but the double/single quotes probably need to be swapped. \$\endgroup\$
    – StarGeek
    Commented May 5, 2023 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.