Renaming Files with Automator - How To Get YYYYMMDD Format?

I'm trying to batch rename files to the date and time they were created using Automator but the only format options seem to be YYYYMD whereas I'm wanting YYYYMMDD (and the same for the time).


I have tried Googling this but none of the results have been helpful.


Is this something that can be done?


I'm trying to rename photos and videos by their creation date/time and I've found that Automator is the only renamer that gets a video's creation date correct (rather than using the date it was imported), at least from the ones I've tried) so I would really like to figure this out.


Thank you.

MacBook Air (M2, 2022)

Posted on Aug 23, 2024 6:10 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 25, 2024 6:55 AM

When you launch Automator and it throws a File Chooser, just click New Document with the Desktop as the location and choose Application.


The first two actions in my example above came from the File & Folders Library. Just drag and drop them in the order shown on the larger workflow panel. The final action (Run Shell Script) came from the Utilities Library.


In the Run Shell Script action, if the Shell is not /bin/zsh, select that. and change Pass input to as arguments. That ensures that the files from the preceding Get Folder Contents will be fed into the Run Shell Script action. Select and remove all of the default code that appears in the Run Shell Script window. Then paste the following:


: <<"COMMENT"
Rename each file so that it is prefaced by its creation date/time. Foo.ext becomes YYYY-mm-dd_HHMMSS_foo.ext.
The rename occurs in the same folder location. The "${@}" is all of the files passed into this action.
The '#' character is a comment symbol in the Shell.
COMMENT

for f in "$@"
do
    # assign YYYY-mm-dd_HHMMSS_ as creation date (%SB) to cdatetime
	cdatetime=$(/usr/bin/stat -t '%F_%H%M%S_' -f '%SB' "${f}")

    # for testing purposes show the full path of the filename with the prefaced creation date
    # ${f:h:r} is the path and ${f:t} is just the original filename 
    # printf '%s%s\n' "${f:h:r}/${cdatetime}${f:t}"

    # rename the original file to its prefaced date/time string
    /bin/mv "${f}" "${f:h:r}/${cdatetime}${f:t}"
done
exit 0


Then save the Automator application to your Desktop and then double-click to open a folder chooser from which to select a single folder containing the files you wish to rename. Use a test folder with a few files first to ensure that the result is acceptible for selecting the real folder.

15 replies
Question marked as Top-ranking reply

Aug 25, 2024 6:55 AM in response to acadiard

When you launch Automator and it throws a File Chooser, just click New Document with the Desktop as the location and choose Application.


The first two actions in my example above came from the File & Folders Library. Just drag and drop them in the order shown on the larger workflow panel. The final action (Run Shell Script) came from the Utilities Library.


In the Run Shell Script action, if the Shell is not /bin/zsh, select that. and change Pass input to as arguments. That ensures that the files from the preceding Get Folder Contents will be fed into the Run Shell Script action. Select and remove all of the default code that appears in the Run Shell Script window. Then paste the following:


: <<"COMMENT"
Rename each file so that it is prefaced by its creation date/time. Foo.ext becomes YYYY-mm-dd_HHMMSS_foo.ext.
The rename occurs in the same folder location. The "${@}" is all of the files passed into this action.
The '#' character is a comment symbol in the Shell.
COMMENT

for f in "$@"
do
    # assign YYYY-mm-dd_HHMMSS_ as creation date (%SB) to cdatetime
	cdatetime=$(/usr/bin/stat -t '%F_%H%M%S_' -f '%SB' "${f}")

    # for testing purposes show the full path of the filename with the prefaced creation date
    # ${f:h:r} is the path and ${f:t} is just the original filename 
    # printf '%s%s\n' "${f:h:r}/${cdatetime}${f:t}"

    # rename the original file to its prefaced date/time string
    /bin/mv "${f}" "${f:h:r}/${cdatetime}${f:t}"
done
exit 0


Then save the Automator application to your Desktop and then double-click to open a folder chooser from which to select a single folder containing the files you wish to rename. Use a test folder with a few files first to ensure that the result is acceptible for selecting the real folder.

Aug 25, 2024 11:13 AM in response to VikingOSX

VikingOSX wrote:

Rename each file so that it is prefaced by its creation date/time. Foo.ext becomes YYYY-mm-dd_HHMMSS_foo.ext.

Thanks for the script. I rename all my images and movies as YYYY-MMDD-hhmm-ss.* with GraphicConverter or exiftool. For practice I modified that script accordingly (see below).


It seems to work OK, but if there are duplicate file creation dates with the same file suffixes, it overwrites the duplicates. That can be prevented by adding -n or -i to the mv command but then it renames only the 1st such duplicate.


It seems other mv variants can bypass this with something like 'mv --backup=numbered' which cause mv to auto-append numbers to the duplicates but that option does not seem to be in macOS Sonoma mv.


for f in "$@"
do
    # assign YYYY-mmdd-HHMM-SS_ as creation date (%SB) to cdatetime
    cdatetime=$(/usr/bin/stat -t '%Y-%m%d-%H%M-%S' -f '%SB' "${f}")

    # ${f:h:r} is the path and ${f:e} is just the original suffix
    # rename the original file to date/time string
    /bin/mv -n "${f}" "${f:h:r}/${cdatetime}.${f:e}"
done
exit 0

Aug 24, 2024 4:16 AM in response to acadiard

It is best to leave slashes and colons out of filenames. Both Finder and the UNIX shell will thank you. Can you live with this date/time preface:


2024-08-24_070000_foo.ext


where the time is exactly 7 am. Looks quite ugly with other separators between hours, minutes, and seconds.


  • Is your batch process selecting a folder and renaming the files in that folder?
    • More than one folder level depth in that primary folder?
    • All files, or specific files bearing a particular extension(s)?


Here is a sample Automator application that prompts you for a folder and applies the date/time creation date (%SB) preface to each found file in that folder. I have the UNIX rename (/bin/mv) commented in my example because I just want to see the applied preface to each file:


/Users/viking/Desktop/Pages_Docs/2024-04-02_100436_text_svg_13_1.pages
/Users/viking/Desktop/Pages_Docs/2024-04-02_100436_ph.pages
/Users/viking/Desktop/Pages_Docs/2024-04-02_100436_eqn.pages
/Users/viking/Desktop/Pages_Docs/2024-04-02_100436_4pager.pages


Aug 24, 2024 10:53 PM in response to VikingOSX

Thank you for your reply.


That date/time works for me. I hadn't really come up with a process yet as I was just testing it out to see if it worked how I was wanting it to, but I couldn't figure out why is wasn't offering double digit months and days when it appears to for others. I couldn't find anyone else with this issue (any screenshot I've seen was double digit) so I thought maybe I was doing something wrong.


I've never used Automator before this so everything in your reply is going right over my head. I tried to replicate your screenshot but nothing happened and I'm not sure if I did something wrong or it was just supposed to be an example...


I did end up finding a program yesterday that renames with the correct creation dates of videos so if I'm going to need to do anything more complicated than my initial attempt with Automator, I think I'll just use the other one.


Aug 25, 2024 11:22 AM in response to Matti Haveri

Your reference to a mv --backup syntax is a GNU mv feature and macOS only offers the BSD mv which I am using.


Since macOS will not permit two identically named files to co-exist in the same directory, it won't matter if they have the same creation date/time as the entire filename with the prepended date/time string cannot be duplicated either.


I chose to make the HHMMSS an integer rather than create more of an eyesore with extra punctuation marks. The %Y that you used is only 24 of 2024, but your choices of formatting are your own.


https://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html

Aug 25, 2024 11:44 AM in response to Matti Haveri

Matti Haveri wrote:


The %Y that you used is only 24 of 2024.

%Y is 2024 (%y is 24). My images are 2001-0101-1200-00.jpg etc.

%Y is replaced by the year with century as a decimal number.
%y is replaced by the year without century as a decimal number (00-99).

https://man.openbsd.org/strftime.3

That was a typo on my behalf and meant %Y. Been coding strftime for decades as far back as UNIX SVR1.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Renaming Files with Automator - How To Get YYYYMMDD Format?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.