Reading EXIF date value to write file creation date with touch (OSX)

Started by pacman, April 24, 2016, 06:22:38 PM

Previous topic - Next topic

pacman

I would like to create a script which will read an image's "DateTimeOriginal" tag ([XMP-exif] DateTimeOriginal) and use that to overwrite that same file's creation date (having posted about this earlier I've been told that being in Mac OSX, EXIFtool doesn't handle such file-related stuff, but luckily I've found the "touch" command to be suitable for this).

First, for displaying the DateTimeOriginal tag:

$ exiftool -DateTimeOriginal FILENAME.PNG

And for writing the file's creation date using an example date of 2012-11-25, 19:15:27 (YYYY-MM-DD, hh:mm:ss) with touch:

$ touch -t "201211251915.27" FILENAME.PNG

Note: a couple of limitations with the "touch" command worth mentioning:

  • The creation date can only only be changed to something earlier than the existing modification date

    •      
    • If the date is set to later than the modification date then only the modification date will be changed
    • If the date is set to earlier than the modification date then both the modification AND creation dates will be changed

As these limitations shouldn't affect my intended task (the dates will always be set to something earlier than the modification date) touch appears to be suitable for the job. Otherwise setfile (part of Xcode, or downloadable separately without Xcode as the 'command line tools' for OSX developers) should work without any limitations as far as I can tell using the following syntax (using the same date as above):

$ setfile -d "11/25/2012 19:15:27" FILENAME.PNG


So, without being a programmer I'm guessing the result of the EXIFtool command can be passed on as a variable to touch in order to overwrite its existing creation date (preferrably comparing the EXIF date with the creation date first, and skipping the process if found to be the same). How can this be done?

I've noticed that the formats are different (mostly the delimiters but also the seemingly lacking timezone in "touch"), so I assume they need "translating" first:
Exiftool displaying "DateTimeOriginal" value:  YYYY:mm:dd HH:MM:SS:Z
(I'm not entirely sure about "Z" as the timezone, but it appears so according to FAQ #5.
Touch command's date format: CCYYMMDDhhmm.ss
("CC" being the century where "CCYY" would be the whole year, i.e. 2012; same as "YYYY" in Exiftool. And I can't see any mention of the timezone, at least not in the OSX 'touch' manual page, nor is there any mention of it in the manual page for the setfile command. Perhaps the existing timezone value will be retained even after the date has been changed....)

Alan Clifford

touch -t `exiftool -s -s -s -d "%Y%m%d%H%M" -datetimeoriginal xf1_8758.jpg` xf1_8758.jpg

From the man page, " The touch utility sets the modification and access times of files", so not the create date.

edit:

But you are creating an earlier date so maybe it will work.

pacman

Quote from: Alan Clifford on April 25, 2016, 07:12:59 AM
touch -t `exiftool -s -s -s -d "%Y%m%d%H%M" -datetimeoriginal xf1_8758.jpg` xf1_8758.jpg

This gave me the error message: touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]
I assume this has to do with the date/time format of touch being slightly different to that of Exiftool. I wasn't familiar with either the -s (short output format) nor -d (date format). I'm currently experimenting with various formats to see if I find one that fits. I should probably look up the exiftool docs for the date formats available.


Quote
From the man page, " The touch utility sets the modification and access times of files", so not the create date.

edit:

But you are creating an earlier date so maybe it will work.

Yes, in this particular case the limitations shouldn't affect me, so I've chosen to try touch first. Setfile uses an even stranger date format, so I thought touch would be better for this.

pacman

I think I've found the correct format for touch to use (I added seconds to your time/date sequence):

$ exiftool -s -s -s -d "%Y%m%d%H%M.%S" -datetimeoriginal testfile.JPG
201501031506.48

However, if I use the above format in the actual command you suggested I still get the same error message:

$ touch -t 'exiftool -s -s -s -d "%Y%m%d%H%M.%S" -datetimeoriginal testfile.JPG' testfile.JPG
touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]

Any idea what I'm doing wrong?



pacman

I tested yet some more....
and found out that the syntax must somehow be wrong (see results below), because;
a) the exiftool command appears to work correctly (outputing a date sequence)
b) the date sequence output from the exiftool command appears to work correctly when manually inserted with the touch -t command
c) ... but both commands together still won't work

$ touch -t 'exiftool -s -s -s -d "%Y%m%d%H%M.%S" -DateTimeOriginal TEST.JPG' TEST.JPG
touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]

$ exiftool -s -s -s -d "%Y%m%d%H%M.%S" -DateTimeOriginal TEST.JPG
201601281424.18

$ touch -t 201601281424.18 TEST.JPG
this command actually changes the file creation and modification dates.

Hayo Baan

Looks like instead of back quotes, you are using regular single quotes. Change the single quotes surrounding the exiftool command with back quotes and you should be fine.
Hayo Baan – Photography
Web: www.hayobaan.nl

pacman

Bingo!!  :D
That quote character did the trick, the corrected command line being:

$ touch -t `exiftool -s -s -s -d "%Y%m%d%H%M.%S" -DateTimeOriginal TEST.JPG` TEST.JPG

Thanks for your help! Much appreciated  :)
Just to avoid any further confusion.... is the above backquote character (`) and the double quote (") character the only ones used with exiftool and/or terminal commands within OSX, and what's the rule for using which one?
I seem to recall having used the regular single quote character (') with exiftool in the past, but I could be wrong.

Thanks again!

Hayo Baan

The way quotes are interpreted is defined by the (UNIX) shell you are using. All three types are used but they have a different meaning.
The back quotes (aka back ticks) execute the surrounded shell command and return the output of that command in its place.
Singe and double quotes are used to group strings (normally the shell splits everything into separate arguments around spaces). Their difference is that double quotes still allow the shell to interprete special characters (e.g., the $ for variables), where the single quotes don't. That's why when wanting to refer to exiftool tag name variables you need single quotes on the exiftool command line, with double quotes the shell would try to interpret your string e.g. $datetimeoriginal as a shell variable instead of passing it as is to exiftool.

Note above is only for UNIX shells, on Windows things work differently...
Hayo Baan – Photography
Web: www.hayobaan.nl