Can exiftool extract portions of the filename?

Started by vbnut, April 13, 2025, 09:06:48 PM

Previous topic - Next topic

vbnut

I use a PowerShell script to ingest images and videos from a memory card to a storage volume on my computer, using exiftool to do the copy, organize them into the same date-based folders and rename them to my personal naming scheme.  This has been working well for a couple of years, but I recently purchased an R5 Mark II, and while the script still works fine for images, videos are a different story. Instead of naming video files similar to images, but with the suffix .mp3, like the R3 did, the R5II uses a more complicated file naming scheme for videos (described here), so my renaming scheme isn't working well for these video files, and I'm trying to decide how to fix it.  As far as I can tell, the various items embedded into the file name are not replicated in the metadata by the camera.

Can exiftool do pattern matching on the filename, perhaps using a regular expression, so that I can write the items in metadata tags and/or create a new file name using some of the items?

StarGeek

Quote from: vbnut on April 13, 2025, 09:06:48 PMCan exiftool do pattern matching on the filename, perhaps using a regular expression, so that I can write the items in metadata tags and/or create a new file name using some of the items?

Yes. The file name is considered a tag, and can be manipulated using any standard Perl function, including regex.

For example, in this post, the YearDateMonth in a filename from WhatsApp are extracted, then set to the Perl default variable to allow writing date/time data into the EXIF time stamps.

It could also be done with Perl regex substitution along these lines
${Filename;s/.*(\d{8}).*/$1/}

Search these forums for "filename regex" to find many examples.

Your main problem, though, is dealing with dollar signs and quoting in PowerShell. The image in this post shows how to quote an exiftool command in CMD and then Mac/Linux. Neither of these commands will work in PowerShell. So you will have to figure out how to make it work, as I can't help with PS.
"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

vbnut

That's great.  Thanks for the info.  I had a 40-year career as a software engineer, and I managed to solve all the quoting issues for my existing -dateFormat parameters (no dollar signs though), so I'm pretty sure I'll be able to figure how to make the regex stuff work in Powershell.  Looks like I get to spend more time in quoting hell :)

vbnut

Just to confirm, can I use filename regexes in the format string parameter to the -dateFormat option?

StarGeek

No. Only text, Common Date Format Codes, and file path codes with doubled percent signs (see the -w (-TextOut) option) can be used in a date format string. No tags names can be used.

If the time stamp needs to be split, then the DateFmt helper function must be used. See Writing "FileName" and "Directory" tags example #13.
"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

vbnut

Hmmm, that's bad news.

Previously I was using the options
"-FileName<CreateDate"
and
-dateFormat "${working_photos_drive}:\Working Photos\%Y Photos\%Y-%m %B\%Y-%m-%d\%Y-%m-%d $event_name_or_location\PSC_%Y%m%d_%%-4f%%+c.%%e"

to rename images (to PSC_20250405_8729.CR3 for example) and copy them to my date-based folder hierarchy (working_photos_drive and event_name_or_location are Powershell variables that were set earlier in the script).

Now for selected videos with names like A_0001C011A250412_160033EJ_CANON.MP4 I want to rename to something like PSC_250412_0001C011.MP4, and after figuring out what %%-4f meant in my pattern above (I had to do some research because I had forgotten), I discovered I could use %%X.Yf to extract the relevant pieces of the filename (characters 3-10) in the new pattern.