ExifTool Forum

ExifTool => Newbies => Topic started by: Jom on March 26, 2020, 07:11:21 AM

Title: How do I write only the last part of the file name to a tag?
Post by: Jom on March 26, 2020, 07:11:21 AM
How do I write only the last part of the file name to a tag?

I have a file with 10 in last part of name
20131111_205640_10.psd
and I only need to write 10 in the tag -exif:usercomment.

Thanks.
Title: Re: How do I write only the last part of the file name to a tag?
Post by: Phil Harvey on March 26, 2020, 08:07:59 AM
To write the last 2 digits:

exiftool "-exif:usercomment<${filename;s/.*(\.{2})\..*/$1/}" DIR

To write the number after the last underline:

exiftool "-exif:usercomment<${filename;s/.*_(\d+)\..*/$1/}" DIR

... there are many other possibilities.

Note that both of these command will write the entire file name if the pattern does not match.  The last command above could be altered like this if you want to write nothing in this case:

exiftool "-exif:usercomment<${filename;s/.*_(\d+)\..*/$1/ or $_ = undef}" DIR

- Phil
Title: Re: How do I write only the last part of the file name to a tag?
Post by: Jom on March 26, 2020, 09:38:59 AM
Thank you, I will try to do this.
Can I use regular expressions within ExifTool without restrictions, as usual?
Title: Re: How do I write only the last part of the file name to a tag?
Post by: Phil Harvey on March 26, 2020, 09:54:38 AM
The only restriction is that braces must be balanced.  See the advanced formatting feature documentation (https://exiftool.org/exiftool_pod.html#Advanced-formatting-feature).

- Phil