I have an old image I want to set the datetimecreated tag from the name.
If I set it to a specific one, it works but not from the name.
C:\Users\x\Desktop>exiftool -DateTimeOriginal="2021:12:08 12:00:00" IMG_20181130_830002.jpg
1 image files updated
C:\Users\x\Desktop>exiftool -overwrite_original "-DateTimeOriginal<${filename;$_ =~ /(\d{4}-\d{2}-\d{2})/ ? $1.' 00:00:00' : ''}" IMG_20181130_830002.jpg
Warning: No writable tags set from IMG_20181130_830002.jpg
0 image files updated
1 image files unchanged
Your regex is looking for 1234-56-78, but your filename doesn't include any dashes.
Just grab the first 8 numbers and pass that directly to exiftool. Exiftool will figure it out and format it correctly (see FAQ #5 (https://exiftool.org/faq.html#Q5), paragraph starting "Having said this...")
exiftool -overwrite_original "-DateTimeOriginal<${filename;$_ =~m/^img_(\d{8})/i;$_=$1} 00:00:00" IMG_20181130_830002.jpg
Though using the conditional is interesting. Just set it to undef on failure instead. And $_=~/<regex> by itself will not change the value of $_. It will return a 0 or 1 depending upon success. You have to assign the result of the conditional operator back into $_
exiftool -overwrite_original "-DateTimeOriginal<${filename;$_=($_ =~m/^img_(\d{8})/i) ? $1.'000000' : undef}" IMG_20181130_830002.jpg
C:\>exiftool -P -overwrite_original "-DateTimeOriginal<${filename;$_=($_ =~m/^img_(\d{8})/i) ? $1.'000000' : undef}" "Y:\!temp\aaaa\IMG_2018 nope 1130_830002.jpg" Y:\!temp\aaaa\IMG_20181130_830002.jpg
Warning: [minor] Advanced formatting expression returned undef for 'filename' - Y:/!temp/aaaa/IMG_2018 nope 1130_830002.jpg
Warning: No writable tags set from Y:/!temp/aaaa/IMG_2018 nope 1130_830002.jpg
1 image files updated
1 image files unchanged
C:\>exiftool -G1 -a -s -DateTimeOriginal "Y:\!temp\aaaa\IMG_2018 nope 1130_830002.jpg" Y:\!temp\aaaa\IMG_20181130_830002.jpg
======== Y:/!temp/aaaa/IMG_2018 nope 1130_830002.jpg
======== Y:/!temp/aaaa/IMG_20181130_830002.jpg
[ExifIFD] DateTimeOriginal : 2018:11:30 00:00:00
2 image files read
Thanks but still doesn't work, I included verbose and the existing meta, if that helps...
C:\Users\Galax\Desktop>exiftool -a -G1 -s IMG_20181130_830002.jpg
[ExifTool] ExifToolVersion : 12.62
[System] FileName : IMG_20181130_830002.jpg
[System] Directory : .
[System] FileSize : 94 kB
[System] FileModifyDate : 2023:05:27 10:30:56+02:00
[System] FileAccessDate : 2023:05:27 10:33:21+02:00
[System] FileCreateDate : 2023:05:26 20:04:57+02:00
[System] FilePermissions : -rw-rw-rw-
[File] FileType : JPEG
[File] FileTypeExtension : jpg
[File] MIMEType : image/jpeg
[File] ExifByteOrder : Little-endian (Intel, II)
[File] CurrentIPTCDigest : b443520a10119da99c2550175e6d0efb
[File] ImageWidth : 1600
[File] ImageHeight : 1200
[File] EncodingProcess : Baseline DCT, Huffman coding
[File] BitsPerSample : 8
[File] ColorComponents : 3
[File] YCbCrSubSampling : YCbCr4:2:0 (2 2)
[JFIF] JFIFVersion : 1.01
[JFIF] ResolutionUnit : None
[JFIF] XResolution : 1
[JFIF] YResolution : 1
[IFD0] Software : Picasa
[IFD0] YCbCrSubSampling : YCbCr4:2:0 (2 2)
[ExifIFD] ExifVersion : 0220
[ExifIFD] DateTimeOriginal : 2021:12:08 12:00:00
[ExifIFD] ColorSpace : sRGB
[ExifIFD] ExifImageWidth : 1600
[ExifIFD] ExifImageHeight : 1200
[ExifIFD] ImageUniqueID : a9d93225f080d2350000000000000000
[InteropIFD] InteropIndex : R98 - DCF basic file (sRGB)
[InteropIFD] InteropVersion : 0100
[IFD1] Compression : JPEG (old-style)
[IFD1] XResolution : 72
[IFD1] YResolution : 72
[IFD1] ResolutionUnit : inches
[IFD1] ThumbnailOffset : 356
[IFD1] ThumbnailLength : 4624
[IFD1] ThumbnailImage : (Binary data 4624 bytes, use -b option to extract)
[XMP-x] XMPToolkit : XMP Core 5.5.0
[IPTC] EnvelopeRecordVersion : 4
[IPTC] CodedCharacterSet : UTF8
[IPTC] ApplicationRecordVersion : 4
[Photoshop] IPTCDigest : b443520a10119da99c2550175e6d0efb
[Composite] ImageSize : 1600x1200
[Composite] Megapixels : 1.9
C:\Users\Galax\Desktop>exiftool -v -overwrite_original "-DateTimeOriginal<${filename;$_ =~m/^img_(\d{8})/i ? $1.'000000' : undef}" IMG_20181130_830002.jpg
======== IMG_20181130_830002.jpg
Setting new values from IMG_20181130_830002.jpg
Warning: No writable tags set from IMG_20181130_830002.jpg
Nothing changed in IMG_20181130_830002.jpg
0 image files updated
1 image files unchanged
My first example should work as is. It captures the regex match and sets $_ equal to that value.
For the conditional, I used the correct format in the example output, but forgot to change it earlier on. It should be fixed now. As I said, $_=~/<regex> does not work. You have to put parenthesis around that so it returns 1 or 0 so that gets passed to the conditional operator. Then you have to assign the whole thing back to the default variable.