How to copy filename and date to rename a folder using Automator?

I have a number of folders with images inside them that were exported from "Photos" app and I would like to rename all the folders to have the same title as the images.


I would like some help to create a workflow or app using Automator to rename all the folders.


So here's my question, how do I get the filename and creation date from the 1st image and then automatically rename the folder to be in the following format:


year-month-day - filename of image


Here's the process I am thinking:


*click application*

prompt for a list of folders to rename

I select the folders

the application will save the filename and creation date in a variable

then it will rename the folder

once complete, it will move onto the next folder until all folders have been renamed


Below are some sample images of how the folder and files look like.


Image 1: Here is the original folder

User uploaded file

Image 2: Within the folder are a list of images. Choose the first image and copy the filename and creation date.

User uploaded file

Image 3: The folder is renamed to the new format.

User uploaded file


Many thanks in advance.

MacBook Pro with Retina display, macOS High Sierra (10.13.6)

Posted on Oct 10, 2018 2:52 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 11, 2018 5:22 PM

I have tested both an AppleScript, and an Automator application solution based on that AppleScript against four different folders — each of which had five sequential filenames in them in the format you shared earlier. Both the AppleScript via Script Editor, and an Automator application successfully changed the folder names to the format that you indicated. On macOS High Sierra 10.13.6 (17G65), and on Mojave 10.14 (18A391).


If you plan to just copy/paste into Script Editor, then the following vertical and horizontally scrollable AppleScript can just be copy/pasted into Script Editor. You would click the compile (hammer) icon, and then could either just run it against a selection of folders to try it out, or save the script as an application, so you could double-click it from the Desktop. Just so you know, I have already copy/pasted this code back into Script Editor and successfully ran it against four folders with success.

-- folder_rename.applescript -- Allows user to select multiple folders, and for each folder, sorts its file content -- by name, and then pick the first filename in that list. It then builds the -- ISO8601 date string (YYYY-MM-DD) from the file's creation date, and then appends -- the extracted filename's name string preceding " - 0000n.ext". Next, it renames -- the selected folder to the newly constructed new folder name. -- Given input filename format: "New Years Day Party - 00001.jpg" whose creation date -- is 2/1/07, the new folder name will be "2007-02-01 - New Years Day Party" -- Version: 1.0 -- Tested: macOS High Sierra 10.13.6 (17G65), Mojave 10.14 (18A391). -- VikingOSX, 2018-10-11, Apple Support Communities, No warranties of any kind. property aDesktop : (path to desktop) as alias property amsg : "Select multiple folders with command key:" -- return all of the base filename preceding " - 00001.jpg" set perlcmd to "'print \"$1\" if /(.*?)(?=\\s-\\s)/; '<<<" try -- press and hold the command key while clicking folders for multiple selection set folder_choices to (choose folder with prompt amsg default location aDesktop with multiple selections allowed without invisibles and showing package contents) on error errmsg number errnbr if errnbr = -128 then display alert "User canceled. Ending script." as critical giving up after 10 end if return end try tell application "Finder" repeat with afolder in folder_choices -- get list of files in folder, sort by name, pick the very first file set first_item to first item of (sort (get files of folder afolder whose kind contains "image") by name) as text as alias -- assume file format: any content up to space-space. Ignore rest of filename. set aname to (do shell script "perl -ne " & perlcmd & (name of first_item as text)'s quoted form) log aname set {year:y, month:m, day:d} to creation date of first_item -- trick to change month and day name to integers set m to m * 1 set d to d * 1 if m < 10 then set m to text -2 thru -1 of ("00" & m * 1) if d < 10 then set d to text -2 thru -1 of ("00" & d * 1) set newFolderName to (y & "-" & m & "-" & d & " - " & aname) as text set the name of afolder to newFolderName -- display dialog newFolderName as text end repeat end tell return


If you plan to use Automator Application instead, then there are some very minor changes needed in the code that are reflected below. You will need the File and Folders library : Ask for Finder Items, and the Utilities library : Run AppleScript action if on High Sierra, otherwise Finder library : Ask For Finder Items, and the Automator library : Run AppleScript if on Mojave. Drag and drop these in that order rightward onto the larger Workflow window.


In the Run AppleScript action, just select everything and remove it — before copying and pasting the following replacement content into it.

property aDesktop : (path to desktop) as alias property amsg : "Select multiple folders with command key:" use scripting additions on run {input, parameters} -- return all of the base filename preceding " - 00001.jpg" set perlcmd to "'print \"$1\" if /(.*?)(?=\\s-\\s)/; '<<<" tell application "Finder" repeat with afolder in input set first_item to first item of (sort (get files of folder afolder whose kind contains "image") by name) as text as alias -- assume file format: any content up to space-space. Ignore rest of filename. set aname to (do shell script "perl -ne " & perlcmd & (name of first_item as text)'s quoted form) log aname set {year:y, month:m, day:d} to creation date of first_item -- trick to change month and day name to integers set m to m * 1 set d to d * 1 if m < 10 then set m to text -2 thru -1 of ("00" & m * 1) if d < 10 then set d to text -2 thru -1 of ("00" & d * 1) set newFolderName to (y & "-" & m & "-" & d & " - " & aname) as text set the name of afolder to newFolderName -- display dialog newFolderName as text end repeat end tell return input end run


Once you have pasted this AppleScript into the Run AppleScript action, click the compile (hammer) button within the action. Save this Automator Action to your Desktop. You run it with a double-click.


Here is a screen shot of the Automator Application workflow. Right-click on the image and save it to your Desktop where you can enlarge it further in Preview.

User uploaded file

Similar questions

16 replies
Question marked as Top-ranking reply

Oct 11, 2018 5:22 PM in response to propertyinvestory

I have tested both an AppleScript, and an Automator application solution based on that AppleScript against four different folders — each of which had five sequential filenames in them in the format you shared earlier. Both the AppleScript via Script Editor, and an Automator application successfully changed the folder names to the format that you indicated. On macOS High Sierra 10.13.6 (17G65), and on Mojave 10.14 (18A391).


If you plan to just copy/paste into Script Editor, then the following vertical and horizontally scrollable AppleScript can just be copy/pasted into Script Editor. You would click the compile (hammer) icon, and then could either just run it against a selection of folders to try it out, or save the script as an application, so you could double-click it from the Desktop. Just so you know, I have already copy/pasted this code back into Script Editor and successfully ran it against four folders with success.

-- folder_rename.applescript -- Allows user to select multiple folders, and for each folder, sorts its file content -- by name, and then pick the first filename in that list. It then builds the -- ISO8601 date string (YYYY-MM-DD) from the file's creation date, and then appends -- the extracted filename's name string preceding " - 0000n.ext". Next, it renames -- the selected folder to the newly constructed new folder name. -- Given input filename format: "New Years Day Party - 00001.jpg" whose creation date -- is 2/1/07, the new folder name will be "2007-02-01 - New Years Day Party" -- Version: 1.0 -- Tested: macOS High Sierra 10.13.6 (17G65), Mojave 10.14 (18A391). -- VikingOSX, 2018-10-11, Apple Support Communities, No warranties of any kind. property aDesktop : (path to desktop) as alias property amsg : "Select multiple folders with command key:" -- return all of the base filename preceding " - 00001.jpg" set perlcmd to "'print \"$1\" if /(.*?)(?=\\s-\\s)/; '<<<" try -- press and hold the command key while clicking folders for multiple selection set folder_choices to (choose folder with prompt amsg default location aDesktop with multiple selections allowed without invisibles and showing package contents) on error errmsg number errnbr if errnbr = -128 then display alert "User canceled. Ending script." as critical giving up after 10 end if return end try tell application "Finder" repeat with afolder in folder_choices -- get list of files in folder, sort by name, pick the very first file set first_item to first item of (sort (get files of folder afolder whose kind contains "image") by name) as text as alias -- assume file format: any content up to space-space. Ignore rest of filename. set aname to (do shell script "perl -ne " & perlcmd & (name of first_item as text)'s quoted form) log aname set {year:y, month:m, day:d} to creation date of first_item -- trick to change month and day name to integers set m to m * 1 set d to d * 1 if m < 10 then set m to text -2 thru -1 of ("00" & m * 1) if d < 10 then set d to text -2 thru -1 of ("00" & d * 1) set newFolderName to (y & "-" & m & "-" & d & " - " & aname) as text set the name of afolder to newFolderName -- display dialog newFolderName as text end repeat end tell return


If you plan to use Automator Application instead, then there are some very minor changes needed in the code that are reflected below. You will need the File and Folders library : Ask for Finder Items, and the Utilities library : Run AppleScript action if on High Sierra, otherwise Finder library : Ask For Finder Items, and the Automator library : Run AppleScript if on Mojave. Drag and drop these in that order rightward onto the larger Workflow window.


In the Run AppleScript action, just select everything and remove it — before copying and pasting the following replacement content into it.

property aDesktop : (path to desktop) as alias property amsg : "Select multiple folders with command key:" use scripting additions on run {input, parameters} -- return all of the base filename preceding " - 00001.jpg" set perlcmd to "'print \"$1\" if /(.*?)(?=\\s-\\s)/; '<<<" tell application "Finder" repeat with afolder in input set first_item to first item of (sort (get files of folder afolder whose kind contains "image") by name) as text as alias -- assume file format: any content up to space-space. Ignore rest of filename. set aname to (do shell script "perl -ne " & perlcmd & (name of first_item as text)'s quoted form) log aname set {year:y, month:m, day:d} to creation date of first_item -- trick to change month and day name to integers set m to m * 1 set d to d * 1 if m < 10 then set m to text -2 thru -1 of ("00" & m * 1) if d < 10 then set d to text -2 thru -1 of ("00" & d * 1) set newFolderName to (y & "-" & m & "-" & d & " - " & aname) as text set the name of afolder to newFolderName -- display dialog newFolderName as text end repeat end tell return input end run


Once you have pasted this AppleScript into the Run AppleScript action, click the compile (hammer) button within the action. Save this Automator Action to your Desktop. You run it with a double-click.


Here is a screen shot of the Automator Application workflow. Right-click on the image and save it to your Desktop where you can enlarge it further in Preview.

User uploaded file

Oct 15, 2018 10:25 AM in response to propertyinvestory

Ok. I have revised the AppleScript to provide folder generation sets when duplicate filenames are found in different folders. I have successfully tested this in plain AppleScript, and again using Automator. With ten folders selected, including five with duplicate filenames, and an empty folder — the code renamed the five Game Night folders as:


2007-10-01 - Game Night, 2007-10-01 - Game Night 2, ... 2007-10-01 - Game Night 5


I also added logic to test for an empty folder, and if true, skip to the next selected folder.


Here is the AppleScript for use in the Script Editor:

-- folder_rename.applescript -- Allows user to select multiple folders, and for each folder, sorts its file content -- by name, and then pick the first filename in that list. It then builds the -- ISO8601 date string (YYYY-MM-DD) from the file's creation date, and then appends -- the extracted filename's name string preceding " - 0000n.ext". Next, it renames -- the selected folder to the newly constructed new folder name. -- Given input filename format: "New Years Day Party - 00001.jpg" whose creation date -- is 2/1/07, the new folder name will be "2007-02-01 - New Years Day Party" -- Version: 2.0, added generation sets to renamed folders to avoid duplicates -- Tested: macOS High Sierra 10.13.6 (17G65), Mojave 10.14 (18A391). -- VikingOSX, 2018-10-15, Apple Support Communities, No warranties of any kind. property aDesktop : (path to desktop) as alias property amsg : "Select multiple folders with command key:" -- return all of the base filename preceding " - 00001.jpg" set perlcmd to "'print \"$1\" if /(.*?)(?=\\s-\\s)/; '<<<" try -- press and hold the command key while clicking folders for multiple selection set folder_choices to (choose folder with prompt amsg default location aDesktop with multiple selections allowed without invisibles and showing package contents) on error errmsg number errnbr if errnbr = -128 then display alert "User canceled. Ending script." as critical giving up after 10 end if return end try tell application "Finder" repeat with afolder in folder_choices repeat 1 times -- check for an empty folder. Get the next folder if it is empty. if items of folder afolder is {} then exit repeat -- get list of files in folder, sort by name, pick the very first file set first_item to first item of (get files of folder afolder whose kind contains "image") as text as alias -- assume file format: any content up to space-space. Ignore rest of filename. set aname to (do shell script "perl -ne " & perlcmd & (name of first_item as text)'s quoted form) set {year:y, month:m, day:d} to creation date of first_item -- trick to change month and day name to integers set m to m * 1 set d to d * 1 if m < 10 then set m to text -2 thru -1 of ("00" & m * 1) if d < 10 then set d to text -2 thru -1 of ("00" & d * 1) set newFolderName to (y & "-" & m & "-" & d & " - " & aname) as text set genset to (1 as integer) set temp to "" -- Produce new named folder generation sets when duplicate filenames are processed -- (e.g. 2007-10-01 Game Night, 2007-10-01 Game Night 2, etc. tell application "Finder" if (exists item ((parent of afolder as text) & newFolderName)) then repeat until not (exists item ((parent of afolder as text) & temp)) set genset to genset + (1 as integer) set temp to newFolderName & space & (genset as text) end repeat set name of afolder to temp else set the name of afolder to newFolderName end if end tell end repeat end repeat end tell return


and here is the AppleScript that replaces the content of the default Automator Run AppleScript action:

-- folder_rename.applescript -- Allows user to select multiple folders, and for each folder, sorts its file content -- by name, and then pick the first filename in that list. It then builds the -- ISO8601 date string (YYYY-MM-DD) from the file's creation date, and then appends -- the extracted filename's name string preceding " - 0000n.ext". Next, it renames -- the selected folder to the newly constructed new folder name. -- Given input filename format: "New Years Day Party - 00001.jpg" whose creation date -- is 2/1/07, the new folder name will be "2007-02-01 - New Years Day Party" -- Version: 2.0, added generation sets to foldernames to avoid duplicate foldernames. -- Tested: macOS High Sierra 10.13.6 (17G65), Mojave 10.14 (18A391). -- VikingOSX, 2018-10-15, Apple Support Communities, No warranties of any kind. property aDesktop : (path to desktop) as alias property amsg : "Select multiple folders with command key:" use scripting additions on run {input, parameters} -- return all of the base filename preceding " - 00001.jpg" set perlcmd to "'print \"$1\" if /(.*?)(?=\\s-\\s)/; '<<<" tell application "Finder" repeat with afolder in input repeat 1 times -- check for an empty folder. Get the next folder if it is empty. if items of folder afolder is {} then exit repeat -- get list of files in folder, sort by name, pick the very first file set first_item to first item of (get files of folder afolder whose kind contains "image") as text as alias -- assume file format: any content up to space-space. Ignore rest of filename. set aname to (do shell script "perl -ne " & perlcmd & (name of first_item as text)'s quoted form) set {year:y, month:m, day:d} to creation date of first_item -- trick to change month and day name to integers set m to m * 1 set d to d * 1 if m < 10 then set m to text -2 thru -1 of ("00" & m * 1) if d < 10 then set d to text -2 thru -1 of ("00" & d * 1) set newFolderName to (y & "-" & m & "-" & d & " - " & aname) as text set genset to (1 as integer) set temp to "" -- Produce new named folder generation sets when duplicate filenames are processed -- (e.g. 2007-10-01 Game Night, 2007-10-01 Game Night 2, etc. tell application "Finder" if (exists item ((parent of afolder as text) & newFolderName)) then repeat until not (exists item ((parent of afolder as text) & temp)) set genset to genset + (1 as integer) set temp to newFolderName & space & (genset as text) end repeat set name of afolder to temp else set the name of afolder to newFolderName end if end tell end repeat end repeat end tell return input end run

Oct 11, 2018 6:31 AM in response to propertyinvestory

Thanks for that information. The code will prompt you for one or more folders, and then loop through them, getting all of the images for a given folder, sorting them by name, and picking the first image of that folder. I have the code to provide the format for the new folder name based on its creation date and extracted filename string. Before the loop processes the next folder, it renames the current folder to the new folder name string.


I want to set up a small test environment with some valid and invalid filenames, and test the code further, before I post here later today. Will have to test on High Sierra and Mojave for thoroughness. Probably after six pm as I have two yards to mow while the weather is finally cooperating.


This is written in AppleScript. So testing will be with Script Editor, and an Automator solution just to ensure consistent behavior. Then I will post the Automator solution for you.

Oct 14, 2018 5:25 PM in response to VikingOSX

Hi VikingOSX,


I've been using this over the last day and it's been working perfectly as expected.


Though, one thing I didn't factor in was the duplication of names. When it finds an image with the same name in a different folder it pops up with an error message.


Could we update this and allow it to rename the folder with the same name and just add an extra number.


Here's an example:


It renames one folder with the following name:


2007-10-20 - Games Night


Then it finds the same image name in a different folder. (this is where an error pops up).


Instead...


We continue to rename the folder with the following:


2007-10-20 - Games Night 2


(simply just add a sequenced number next to it)


Is this possible to do?


Thank you,

Oct 14, 2018 6:10 PM in response to propertyinvestory

I had wondered if the duplicate folder naming issue would rear its head, and now we know. Also, I believe that sorting a folder of same name files that only differ by sequence number is unnecessary processing too, since we don't use the sequence number. So two issues.


It is simple to add newly formed folder names to another list, after checking for list membership (duplicate). Keeping track of the count of duplicates is more involved, but I think I can work that out too.


Something that may confound things is the normal filename that ends in a space with trailing number (e.g. Game Night 2 - 00001.jpg, and ends up as a folder 2007-10-20 - Game Night 2). I can't increment that, and adding another space and 2 to that will be confusing.


I will look at this on Monday, and when I have another solution, I will post back.

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.

How to copy filename and date to rename a folder using Automator?

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