Regex filename to take digits between the 2 hyphens to extract date?

Started by xiftheflour, November 15, 2019, 09:33:45 AM

Previous topic - Next topic

xiftheflour

Hi all, trying to use EXIFTOOL to extract the date from the filename so that I can write the date upon photo import.

All my filenames are int he same format (Whatsapp videos)...side note, not sure why they all have a 0000:00:00 date creation....

Anyway the format is:
VID-YYYYMMDD-WA0000.MP4  (the 0000 are any numbers)

I'm thinking I can do this a few ways (like just get the substring of year/month/day) but what will be useful for later is to learn to extract the part in between the hyphens.

Wondering if anyone can help:


  • With the code to extract the YYYYMMDD by looking between the two hyphens?
  • With the substring code to break up the YYYY, MM, DD
  • With the regex to use the above to parse into the date?

Just playing around I think I have something like:

exiftool "-datetimeoriginal<${filename ...... }000000" "VID-20190119-WA0005.mp4"

Phil Harvey

Try this:

exiftool "-datetimeoriginal<${filename;s/WA.*//}000000" DIR

This command removes everything from the "WA" in the file name onwards.  The only reason this is necessary is to avoid the numbers after the WA from being interpreted as the hours/minutes.  ExifTool is quite flexible about date/time formats (see FAQ 5).

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

xiftheflour

Quote from: Phil Harvey on November 15, 2019, 09:59:24 AM
Try this:

exiftool "-datetimeoriginal<${filename;s/WA.*//}000000" DIR

This command removes everything from the "WA" in the file name onwards.  The only reason this is necessary is to avoid the numbers after the WA from being interpreted as the hours/minutes.  ExifTool is quite flexible about date/time formats (see FAQ 5).

- Phil

Perfect! Works exactly how I wanted it!

Mind if I ask what the ;s/ and the .*// does?

Pretty cool how it knows how to pick up the dates!

StarGeek

s/WA.*// is a Regular Expression (RegEx) substitution.  It substitutes what is matched between the first and second slashes with what is between the second and third slashes. The dot stands for any single character and the asterisk modifies the dot so that it matches zero or more characters.  So it will match "WA" followed by zero or more of any character.  Since there is nothing between the second and third slashes, the match is replaced with nothing.

RegEx is a complex subject all by itself and there are plenty of tutorials on the web if you wish to learn more.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).