Author Topic: How do I write only the last part of the file name to a tag?  (Read 142 times)

Andrei Korzhyts

  • Sr. Member
  • ****
  • Posts: 236
How do I write only the last part of the file name to a tag?
« 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.

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 17049
    • ExifTool Home Page
Re: How do I write only the last part of the file name to a tag?
« Reply #1 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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Andrei Korzhyts

  • Sr. Member
  • ****
  • Posts: 236
Re: How do I write only the last part of the file name to a tag?
« Reply #2 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?

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 17049
    • ExifTool Home Page
Re: How do I write only the last part of the file name to a tag?
« Reply #3 on: March 26, 2020, 09:54:38 AM »
The only restriction is that braces must be balanced.  See the advanced formatting feature documentation.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).