Creating datetime from nonstandard filename

Started by salis500, July 16, 2021, 10:05:10 PM

Previous topic - Next topic

salis500

I haven't coded in years, so total newb.  My filenames are in the format "Month-Day-Year Hour-Min-Sec AM/PM.jpg".  I have several files that have no DateTimeOriginal.  I know of "-datetimeoriginal<filename" but that doesn't work due to my filename date format and time being in 12 hr format.  I'm just having problems getting the parameters set properly to read the name to create a YYYY:MM:DD H:M:S date.

Also, would it be easy with some sort of if command that could check all the pictures in a folder for presence of a ceratin date, and if not, create it from the name.

Sorry for the newb question, any help would be appreciated.

Phil Harvey

You could maybe try something like this:

exiftool "-datetimeoriginal<${filename;my @a=/\d+/g;$a[3]+=12 if /PM/ and $a[3]!=12;$a[3]=0 if /AM/ and $a[3]==12;$_=qq(@a[2,0,1,3,4,5])}" DIR

The AM/PM is a pain for 12:00.  Here I have set 12:00 AM to 00:00 (24 hr), and 12:00 PM to 12:00 (24 hr), which I think is correct.

There are other ways to do this using the -d option, but the AM/PM may be a problem there too.

- 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 ($).

StarGeek

Quote from: Phil Harvey on July 16, 2021, 10:31:19 PM
There are other ways to do this using the -d option, but the AM/PM may be a problem there too.



C:\>exiftool -P -overwrite_original -d "%m-%d-%Y %I-%M-%S %p.jpg" "-DateTimeOriginal<filename" "Y:\!temp\bbb\07-16-2021 12-00-00 PM.jpg" "Y:\!temp\bbb\07-16-2021 12-00-00 AM.jpg"
    2 image files updated

C:\>exiftool -G1 -a -s -DateTimeOriginal "Y:\!temp\bbb\07-16-2021 12-00-00 PM.jpg" "Y:\!temp\bbb\07-16-2021 12-00-00 AM.jpg"
======== Y:/!temp/bbb/07-16-2021 12-00-00 PM.jpg
[ExifIFD]       DateTimeOriginal                : 2021:07:16 12:00:00
======== Y:/!temp/bbb/07-16-2021 12-00-00 AM.jpg
[ExifIFD]       DateTimeOriginal                : 2021:07:16 00:00:00
    2 image files read


It's only because I was pretty sure I remembered that you did did it this way at some point.
* 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).

Phil Harvey

Nice. That's much easier than what I did.

- 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 ($).

salis500

That worked, thanks.  I don't like that the filenames are in the am/pm format, but back in the day picasa did that so kept using it that way.  I've been wondering if it'd just be easier to convert them to a standard 24hr format to make things easier in the future.