Add Dates To File Or Photo Names With an Automator Script

You can use a shell script inside an Automator Quick Action to add the date of a file to the beginning of the file's name. You can also make this work with photos using the date the photo was taken from the metadata in the file. As a Quick Action you can easily apply this name change to one or many files at once.



Shell script for renaming by file date:

for f in "$@"
do
	filedate=$(date -r $(stat -f %B $f) +%Y-%m-%d);
	filename=$f:t
	filepath=$f:h
	mv $filepath/{"$filename","$filedate $filename"}
done

Shell script for renaming by photo metadata date:

for f in "$@"
do
	filedate=$(mdls $f | grep kMDItemContentCreationDate -m 1 | awk '{print $3}');
	filename=$f:t
	filepath=$f:h
	mv $filepath/{"$filename","$filedate $filename"}
done

Comments: 16 Responses to “Add Dates To File Or Photo Names With an Automator Script”

    Dana Stevens
    5 years ago

    Felt obligated to comment since this is just the kind of content I enjoy. Someday I'll be looking for a way to get a photo's taken date into a script and I now have a solution. Thanks, Gary.

    David Sofi
    5 years ago

    Excellent tip; I've just renamed more than 6,000 photos I couldn't organize by year without this. I tripped over trying to type in the terminal work before discovering I only needed to copy/paste your Automator Script. A+

    Graham Watson
    5 years ago

    Fantastic solution, Gary, and you have managed to introduce these complex concepts in an easily-understandable way. Thank you!

    Dan Stone
    5 years ago

    How can I modify the shell script to have the filedate but not including the original filename as the output.
    In other words, I would like to original file to be renamed as: 12-27-1995.jpg

    5 years ago

    Dan: Instead of the renaming going to "$filedate $filename" just have it as "$filedate.jpg"

    Al Cheshinski
    5 years ago

    When I run it the name moves one space to the right and stops.

    Al Cheshinski
    5 years ago

    It works on external discs. I have a personal cloud Drive that it is not working on must be some kind of permission problem. Thanks for the info.

    Peter Knight
    5 years ago

    Thanks Gary, this is exactly what I've been looking for. A couple of questions though.

    i) I can't work out how to concatenate the date and the time with an underscore separating the two.
    ii) The date seems to be returned as a UTC value, not what is in the EXIF info
    iii) Rather than having colons as a separator between the hours minutes and seconds, forward slashes (/) are substituted
    iv) How can I remove the separators in both the date and time fields?

    Many thanks.

    JIm Rietz
    5 years ago

    I'm getting spaces like Al was. When I do mdls on a file directly by dropping it into terminal, there are only 15 constants displayed as null or empty and no kMDItemContentCreationDate listed. I'm running Mojave and suspect t his may be a Catalina only command.

    Charles Tutt
    5 years ago

    Very useful! Can't wait to try it! Thanks Gary.

    5 years ago

    Peter: That's going to take some string processing in your script. It is all possible, but only if you take the time to learn some more programming. Alternatively, you could use a regular batch rename in the Finder to do things like replace spaces with _. UTC is probably what is really in the EXIF info, you just see a converted value in Preview. I'm thinking there is probably a way to convert the string date in the shell script too, but it will take more research.

    Gene
    5 years ago

    Good video Gary. Can you do this inside the Photos App?

    5 years ago

    Gene: No. You can't even set the file name of a photo manually inside the Photos app.

    Dennis
    5 years ago

    This is nice and a great explanation. Can Automator be used to stuff the 'real' date of a photo into the metadata field for the date the picture was taken? I've scanned pictures and they have a created/modified date in 2019 but I would like to identify them with the real point in time for which the picture was taken (i.e., our wedding date.)

    5 years ago

    Dennis: Possibly. But it will get a lot more complex. A Google search came up with some things, but I haven't investigated them.

    Andreas
    5 years ago

    Congratulations Gary, this is what I've been looking for since apple abandoned Aperture. (In Aperture, there was a sophisticated renaming tool for files upon import).

Comments Closed.