ExifTool Forum

ExifTool => Newbies => Topic started by: PlasticArmyGuy on April 20, 2025, 12:58:03 PM

Title: Parse year from basename
Post by: PlasticArmyGuy on April 20, 2025, 12:58:03 PM
I have multiple scanned photos with a year in the filename.  For example, a file may be named Tom_graduation_1952_photo.jpg or Mustang_Sally_1968.jpg.  The year can appear anywhere within the name.

I have been trying without success to parse the four digits and insert that into the subject tag. 

Can this be done and if so, how?

Appreciate any assistance.
Title: Re: Parse year from basename
Post by: StarGeek on April 20, 2025, 01:06:30 PM
Try
exiftool "-Subject+<${Filename;m/(\d{4})/;$_=$1}" /path/to/files/

This command will grab four consecutive numbers and add that to the XMP-dc:Subject, which is used to hold keywords.
Title: Re: Parse year from basename
Post by: PlasticArmyGuy on April 20, 2025, 01:36:51 PM
Thank you.  That works well.

Would it be possible to modify this to skip files that do not have a date in the name?
Title: Re: Parse year from basename
Post by: StarGeek on April 20, 2025, 01:49:06 PM
Try
exiftool -if "$filename=~m/(\d{4})/" "-Subject+<${Filename;m/(\d{4})/;$_=$1}" /path/to/files/
Title: Re: Parse year from basename
Post by: PlasticArmyGuy on April 20, 2025, 01:56:29 PM
Thanks again.  Works great!