How To Change File Creation Dates To Match Photo Metadata

When you export photos from the Photos app the date of the file doesn't match the date the photo was taken. This is correct as the file is new and the photo in it does retain the metadata with the original date. But if you want to force the file date to match the photo date, you can do it in the Terminal or with an Automator app.



Here’s the code used in this tutorial:

for f in "$@"
do
	touch -t $(date -j -f "%Y%m%d%H%M" -v-6H $(mdls $f | grep kMDItemContentCreationDate | head -n1 | awk '{gsub("[^[:digit:]]+"," ");print $1$2$3$4$5}') +%Y%m%d%H%M) $f
done

Comments: 33 Responses to “How To Change File Creation Dates To Match Photo Metadata”

    Douglas Brace
    3 years ago

    Is it possible to do this so that the creation date is changed but not the modified date? Why am I asking? Technically speaking someone might want the creation date to be the actual original date and time from when the photo was taken but might want the modified date to be when the photo was exported (in this case, since the original was an HEIC file the export would be the JPEG version of it). If it is possible, thanks in advance. If not, I completely understand and I appreciate everything.

    3 years ago

    Douglas: That will take much more work. You'd have to save the original modified date to a variable, then recall it and set the modified date after setting the primary date.

    Dan
    3 years ago

    Amazing Thank You!
    Just what I was looking for. All other guides was only about changing the date manually one file at a time.
    With this Automator I can now change thousands of files by select and drag.

    iPhone Photo app seems not to care about true metadata, and uses the file created date instead, when transferring backed up photos and videos back to iPhone.

    Craig
    3 years ago

    Will this work for .mov files?

    3 years ago

    Craig: Theoretically yes. But video file metadata is different so you’ll need to research the correct things to address and perhaps do some trial and error.

    Victor
    3 years ago

    Hi,
    could you tell me why the renamed folder shows hour from metadata with 2 hours less than the real hour?
    In the Exif data from my photos (macOS Big Sur) it shows: "Date Time Original: 17 Oct 2020 at 18:56:49", and more below: "Time Zone for Original Date: +02:00". If I run your script it shows in the renamed folder name with 16:56:49".

    Thanks for answers/solutions.

    Kind regards

    Victor

    3 years ago

    Victor: I suppose because it is adjusting for time zone. Add 2 hours if you like.

    Rami
    3 years ago

    Hi - The challenge I have is, when I run the command (mdls) for my photo the result are:

    kMDItemContentCreationDate = 2017-11-02 20:07:18 +0000
    kMDItemContentModificationDate = 2018-12-17 21:07:18 +0000

    (kMDItemContentCreationDate) is not accurate, the (kMDItemContentModificationDate) is the accurate one.
    How I can update the file created date to match (kMDItemContentModificationDate) instated of (kMDItemContentCreationDate) ?

    Thanks for supporting

    3 years ago

    Rami: Did you try just changing the code to use the property you want?

    Rami
    3 years ago

    Yes, Here is the code I used,
    for f in "$@"
    do
    touch -t $(date -j -f "%Y%m%d%H%M" -v-8H $(mdls $f | grep kMDItemContentModificationDate | head -n1 | awk '{gsub("[^[:digit:]]+"," ");print $1$2$3$4$5}') +%Y%m%d%H%M
    202105271859) $f
    done

    However, the code above changed the "file modification date" not the "creation date" any thoughts?
    Also, any thoughts on why the kMDItemContentCreationDate does not match kMDItemContentModificationDate?

    3 years ago

    Rami: Not sure why it isn't working for you. Just keep researching and debugging. I don't have any more advice off the top of my head right now on this.

    Paweł
    3 years ago

    I used this script many times, but now I have an error "touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]”.

    the script:
    for f in "$@"
    do
    touch -t $(date -j -f "%Y%m%d%H%M" -v+1H $(mdls $f | grep kMDItemContentCreationDate | head -n1 | awk '{gsub("[^[:digit:]]+"," ");print $1$2$3$4$5}') +%Y%m%d%H%M) $f
    done

    I don't now how to fix it?

    3 years ago

    Paweł: probably something up with the date of a file you are trying this with. Inspect those photo dates carefully or have it just echo the date in this script if you want (not sure if you are a coder or not).

    Russ
    3 years ago

    To fix the error: touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]”

    Replace the curly quotes with the non-curly quotes.

    Paweł
    3 years ago

    I reset my computer and the error is gone.

    Dan
    3 years ago

    Hi again...
    I am having problem with not being able to set a creation date if it's earlier than the current creation date of the file.

    I have some files with the same creation date of 2000 but they are taken in 2020..
    kMDItemContentCreationDate = 2000-01-01 00:00:00 +0000
    kMDItemFSCreationDate = 2000-01-01 00:00:00 +000

    kMDItemContentModificationDate = 2020-08-29 16:12:00 +0000
    kMDItemFSContentChangeDate = 2020-08-29 16:12:00 +0000

    So is there really no way to set this right?

    3 years ago

    Dan: I think you should be able to. Experiment. You could always open them in an app and export out new files and try to change those.

    Elissa
    3 years ago

    I am following the video to a T, but on my macbook when I put "pass input" as "as arguments" I do not get the bits with an "f" instead it automatically shows this

    while ( $# )
    echo "$1"
    shift
    end

    I tried simply copy-pasting from your site into the automator but it says "illegal variable name" when I try to run it.

    3 years ago

    Elissa: Look at the Shell setting there too. Make sure it is the same as mine.

    Elissa
    3 years ago

    Thank you! It now shows the correct starting position. I copy pasted the script and tried to alter a photo but it only changed the modified time-stamp to two hours later from the created time. (Metadata is still untouched and safely preserved with the correct original date time though thankfully!)

    Elissa
    3 years ago

    Me again! It seems that if I run the files through them two or three times, then it works!! Some jpegs don't work, but I'll take it! -- Is there any way to do the same thing but with .mov or .mp4 files?? Thank you!!

    3 years ago

    Elissa: That's odd that it should take more than one time. Are you doing this in the Terminal, or Automator? Try just using the Terminal to see if that makes a difference. Also, are these files in a cloud service, or local? I wonder if that is affecting it for you.
    As for videos, that's different. Most of the videos I see don't even have a date in the metadata. No idea what to do there to get what you want, sorry.

    Elissa
    3 years ago

    I'm using the automator-made-app! The files are local, in finder in a folder. Before, (within the last 2 years), if I airdropped photos/videos from my iphone, the date was capture time/date so when I moved them to folders it was neatly organized by date. Now they're all under the date that I airdrop them so everything is a mess... You have been an absolute lifesaver and I am so grateful for you and this site.

    Jason
    3 years ago

    Great info, thanks Gary! Can this be adapted so that we use the original date/time to append the filename of the file instead of changing the file create date? e.g. if I have DSC_0143.NEF photographed Jul 20, 2008 at 3:59:33 PM (PDT, so -7) but the file create date is wrong, as long as I can rename this file to "2008-07-20 1559 DSC_0143.NEF" then I care less what the actual Created Date time is.

    3 years ago

    Jason: Possibly. You'll have to play around with it and see what you can do. I don't know offhand.

    Fabrice
    3 years ago

    Nice tutorial, but problem is on Photos that uses date file instead of exif dates to sort pictures.
    Each time I modify a picture with a third app, date in exif is still correct (eg. in 1984) but file date change to the current date (eg. in 2022). Then in Apple Photos this pictures is displayed in year 2022 instead of 1984.
    So using this script would be nice, but recently I change my iPhone, each time I plug it on my mac photos are duplicated... I don't know why ! (icloud ?)
    Worst Apple App !

    3 years ago

    Fabrice: If you want to change the date in the Photos app, you can do that easily in the Image menu. If you are using iCloud Photos you should never import your photos when you connect your iPhone to your Mac. Just turn that option off as there is no need to sync your photos AND use iCloud at the same time.

    Todd
    2 years ago

    Hi Gary. Thank you for this wonderful tutorial. I have been trying to create this Automator Application to work on videos using the ExifTool. So far I came up with this:

    [CODE]touch -t `exiftool -CreateDate -d "%Y%m%d%H%M.%S" -s3 "/path/to/file.mp4"` "/path/to/file.mp4"[/CODE]

    It works when typed into the terminal but I can't figure out how write the Shell Script for the Automator Application. Im mostly not sure how to deal with the file path. Do you think you can help please. Thank you.

    2 years ago

    Todd: The example in this video uses a parameter for that. You see that passed in as $@ and then the loop will assign each file to $f. Try to fully understand how this works in my example before trying to apply it to a different shell command. Perhaps team up with a colleague why knows shell scripts if you don't have the experience yet.

    Dermott D
    2 years ago

    hi is there a possibility of using kMDItemFSName as the file creation date, as this is the correct creation date but with transferring files back and forth from a few different drives the creation date shows the date I transferred them to the computer.

    thank you

    2 years ago

    Dermott: Do you mean set the name of the file to include the date? I suppose you could do that if you wanted. I regularly name my tutorial projects with the date in the name.

    Dan
    2 years ago

    Hi
    I have found a better solution for setting the created date as the same as modified date. Your method doesn't work for me when the files got a created date 2000-01-01, which it will become after backing up from iPhone to my server with a dedicated app from the manufacturer.

    The command SetFile seems to be a better option on all levels, and you don't even have to think about the hour adjustment.

    for f in "$@"
    do
    SetFile -d "$(GetFileInfo -m $f)" $f
    done

    2 years ago

    Dan: That's a good solution but it only works if you have Xcode and the Xcode command line tools installed. Most people don't.

Comments Closed.