Changing dates based on filename with regexp formatting

Started by TinCanFury, December 04, 2024, 11:32:07 PM

Previous topic - Next topic

TinCanFury

I have a bunch of photo files with names like "17-10-31 21-48-09 2525" followed by the extension.
It's "Year-Month-Day Hour-Min-Sec" followed by what seems to be a random number.

so I'm trying to use,
exiftool -v "-alldates<${filename;$_=substr($_,0,16);s/(\d{2})-(\d{2})-(\d{2}) (\d{2})-(\d{2})-(\d{2})/$1:$2:$3 $4:$5:$6/}" <filename>

however I get the error,

-bash: -alldates<${filename;$_=substr($_,0,16);s/(\d{2})-(\d{2})-(\d{2}) (\d{2})-(\d{2})-(\d{2})/20$1:$2:$3 $4:$5:$6/}: bad substitution
and not sure what I'm doing wrong.

thanks for the help!

StarGeek

The only thing missing from the date/time is the 20 before 2017, correct?

See FAQ #5, paragraph starting "Having said this..."

All that is needed is to add the 20 in front of the file name. Also, since you appear to be on Mac/Linux, you need to put single quotes around any parameter with a dollar sign in it

Try
exiftool '-AllDates<20$Filename' /path/to/files/

Example (using double quotes since I'm on Windows)
C:\>exiftool -P -overwrite_original "-AllDates<20$Filename" "Y:\!temp\x\y\17-10-31 21-48-09 2525.jpg"
    1 image files updated

C:\>exiftool -G -a -s -AllDates "Y:\!temp\x\y\17-10-31 21-48-09 2525.jpg"
[EXIF]          DateTimeOriginal                : 2017:10:31 21:48:09
[EXIF]          CreateDate                      : 2017:10:31 21:48:09
[EXIF]          ModifyDate                      : 2017:10:31 21:48:09
"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

TinCanFury

#2
Thanks for your response!
ah, oops, apparently copied the command where I didn't have the 20 in the regexp like this one,

exiftool -v "-alldates<${filename;$_=substr($_,0,16);s/(\d{2})-(\d{2})-(\d{2}) (\d{2})-(\d{2})-(\d{2})/20$1:$2:$3 $4:$5:$6/}" <filename>

(I was trying a bunch of them to try and figure out where I was going wrong.
But this one also doesn't work.

From your answer it looks like I don't need to change the - to : then? Is there any reason I was getting the "bad substitution error"? am I using the regexp replace incorrectly?

Thanks!

TinCanFury

Ah, your double quote comment made me realize I quoted it wrong...

$ exiftool -v '-createdate<${FileName;$_=substr($_,0,16);s/Photo\ //;s/(\d{2})-(\d{2})-(\d{2}) (\d{2})-(\d{2})-(\d{2})/20$1:$2:$3 $4:$5:$6/}' 23-08-26\ 18-25-02\ 7203.jpg
======== 23-08-26 18-25-02 7203.jpg
Setting new values from 23-08-26 18-25-02 7203.jpg
Warning: No writable tags set from 23-08-26 18-25-02 7203.jpg
Nothing changed in 23-08-26 18-25-02 7203.jpg
    0 image files updated
    1 image files unchanged

thank you for the insight!

TinCanFury

#4
I ran across a bunch of them that started with "Photo ", made this slight modification to the substr command, and this worked!

$ exiftool -P -v -progress -'alldates<${FileName;$_=substr($_,6,22);s/(\d{2})-(\d{2})-(\d{2}) (\d{2})-(\d{2})-(\d{2})/20$1:$2:$3 $4:$5:$6/}' Photo\ 23-10-27\ 02-31-05\ aa18.jpg
======== Photo 23-10-27 02-31-05 aa18.jpg [1/1]
Setting new values from Photo 23-10-27 02-31-05 aa18.jpg
Rewriting Photo 23-10-27 02-31-05 aa18.jpg...
  Editing tags in: APP0 APP1 CIFF ExifIFD IFD0 JFIF MIE-Doc MakerNotes PDF PNG PostScript QuickTime UserData XMP
JPEG APP0 (14 bytes):
  Rewriting JFIF
Creating APP1:
  Creating IFD0
  Creating ExifIFD
JPEG DQT (65 bytes):
JPEG DQT (65 bytes):
JPEG SOF2:
JPEG DHT (24 bytes):
JPEG DHT (23 bytes):
JPEG SOS
    1 image files updated

Which got me to understand I had the substr command wrong,

3$ exiftool -P -v -progress -'alldates<${FileName;$_=substr($_,0,17);s/(\d{2})-(\d{2})-(\d{2}) (\d{2})-(\d{2})-(\d{2})/20$1:$2:$3 $4:$5:$6/}' 23-10-13\ 13-04-14\ 7524.jpg
======== 23-10-13 13-04-14 7524.jpg [1/1]
Setting new values from 23-10-13 13-04-14 7524.jpg
Rewriting 23-10-13 13-04-14 7524.jpg...
  Editing tags in: APP0 APP1 CIFF ExifIFD IFD0 JFIF MIE-Doc MakerNotes PDF PNG PostScript QuickTime UserData XMP
JPEG APP1 (294 bytes):
  Rewriting IFD0
  Rewriting ExifIFD
JPEG APP2 (550 bytes):
JPEG DQT (130 bytes):
JPEG DRI (2 bytes):
JPEG SOF0:
JPEG DHT (416 bytes):
JPEG SOS
    1 image files updated

huzzah!

StarGeek

I would suggest using this, as it is much simpler.
exiftool '-AllDates<20${Filename;tr/0-9//cd;}' /path/to/files/

The Perl tr command removes everything that is not a number. That means that Filename (as a tag value, this doesn't change the actual file's name) is changed to 23102702310518. The 20 is added to the front, exiftool grabs the first 14 numbers and drops the trailing 18.

Example
C:\>exiftool -P -overwrite_original "-AllDates<20${Filename;tr/0-9//cd;}" "Y:\!temp\x\y\Photo 23-10-27 02-31-05 aa18.jpg"
    1 image files updated

C:\>exiftool -G -a -s -AllDates "Y:\!temp\x\y\Photo 23-10-27 02-31-05 aa18.jpg"
[EXIF]          DateTimeOriginal                : 2023:10:27 02:31:05
[EXIF]          CreateDate                      : 2023:10:27 02:31:05
[EXIF]          ModifyDate                      : 2023:10:27 02:31:05

Personally, when setting a timestamp, I almost never format it "correctly". It's much easier to type the numbers out and let exiftool figure it out
C:\>exiftool -P -overwrite_original -AllDates=20241205120000 y:\!temp\Test4.jpg
    1 image files updated

C:\>exiftool -G -a -s -AllDates y:\!temp\Test4.jpg
[EXIF]          DateTimeOriginal                : 2024:12:05 12:00:00
[EXIF]          CreateDate                      : 2024:12:05 12:00:00
[EXIF]          ModifyDate                      : 2024:12:05 12:00:00
"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