Sort files into folders by date created using Automator

New to automator and I found this thread (Sort files into folders by date created - Apple Community) which already has a script with everything I'm looking for but I cannot get it to work. I want to:

Sort files by date taken

Create folders by day - yyyy-mm-dd

How do I successfully launch the application I've created on my desktop? It only lets me choose one file and the script does not run. Please help.



MacBook Pro 13″, macOS 10.12

Posted on May 1, 2022 11:12 PM

Reply
Question marked as Top-ranking reply

Posted on May 2, 2022 6:09 AM

Let's review how you set up the two Automator actions in the Automator Application. The input is a single folder, so you have to configure that in the Ask for Finder Items action:



This will ask for a single folder and the path of that folder will be passed to the Run Shell Script action that follows:



You must select the correct Shell entry and because you are passing a Folder path, the Pass input: must be as arguments.


There will be a boilerplate Ruby code loop in that action, and you will remove that, before copying/pasting the full Ruby code that I wrote into it. If you open that Ruby code in TextEdit, you will destroy the sensitive indent that is necessary for correct code execution. That is why I suggest a couple of programmer's editors in the documentation because they won't harm the code before copy/pasting into the Run Shell Script action.


Look at the extensions set in the code. There is a bug in there. The 'dng' extension should be '.dng', so it will process DNG image files. If there are any extensions missing that you require, add them in the exact lowercase syntax shown.


You can test run the Automator application before you save it to your Desktop by clicking the Run button. It will pop a dialog that you can ignore and then it will prompt you for a folder containing your images. The next thing you see will be the final tally of files, folders, and elapsed time. Your YY-mm-dd folders will contain their respective images in the designated folder.


The same code (v2.0) that I posted in 2019 runs without change on macOS 11.6.5 and with the Apple supplied Ruby v2.6.3p62. It also works with Ruby v3.1.2 that I have separately installed. It works whether you have installed the free third-party tool ExifTool in /usr/local/bin, or not.


Apple stated in the past that they planned to remove Python, Ruby, Perl, and PHP from future operating systems. With macOS 12.3.1, they have now removed PHP, and Python and only Apple knows when they will remove Ruby and Perl. As this solution depends on Ruby, it will break on the operating system release where Apple has removed Ruby, unless one installs their own current version of Ruby. I will give some thought to the practicality of rewriting this script in Zsh, which Apple has committed to as the default shell.

Similar questions

6 replies
Question marked as Top-ranking reply

May 2, 2022 6:09 AM in response to Rach_22

Let's review how you set up the two Automator actions in the Automator Application. The input is a single folder, so you have to configure that in the Ask for Finder Items action:



This will ask for a single folder and the path of that folder will be passed to the Run Shell Script action that follows:



You must select the correct Shell entry and because you are passing a Folder path, the Pass input: must be as arguments.


There will be a boilerplate Ruby code loop in that action, and you will remove that, before copying/pasting the full Ruby code that I wrote into it. If you open that Ruby code in TextEdit, you will destroy the sensitive indent that is necessary for correct code execution. That is why I suggest a couple of programmer's editors in the documentation because they won't harm the code before copy/pasting into the Run Shell Script action.


Look at the extensions set in the code. There is a bug in there. The 'dng' extension should be '.dng', so it will process DNG image files. If there are any extensions missing that you require, add them in the exact lowercase syntax shown.


You can test run the Automator application before you save it to your Desktop by clicking the Run button. It will pop a dialog that you can ignore and then it will prompt you for a folder containing your images. The next thing you see will be the final tally of files, folders, and elapsed time. Your YY-mm-dd folders will contain their respective images in the designated folder.


The same code (v2.0) that I posted in 2019 runs without change on macOS 11.6.5 and with the Apple supplied Ruby v2.6.3p62. It also works with Ruby v3.1.2 that I have separately installed. It works whether you have installed the free third-party tool ExifTool in /usr/local/bin, or not.


Apple stated in the past that they planned to remove Python, Ruby, Perl, and PHP from future operating systems. With macOS 12.3.1, they have now removed PHP, and Python and only Apple knows when they will remove Ruby and Perl. As this solution depends on Ruby, it will break on the operating system release where Apple has removed Ruby, unless one installs their own current version of Ruby. I will give some thought to the practicality of rewriting this script in Zsh, which Apple has committed to as the default shell.

May 4, 2022 9:29 AM in response to Rach_22

The script was written with the original intent that a unique folder would be selected with a bunch of images in it and processing would place every file in its creation date (YYYY-MM-DD) named folder in that specific parent folder location. It was not written to be used repetitively on the same parent folder with a new batch of images copied to it. Though it would still handle existing folders or create new folders, it does not deal with duplicate filenames, duplicate filenames whose contents may be different, or how to add generation (e.g. _001, _002) sets to the duplicate filename(s) to uniquely preserve them in their respective folder. This would likely add half-again more code to the script and hours of testing. I don't have the available time to do this.


Instead, I recommend that you use a unique parent folder with the files that you wish to process. Let's say that you end up with a folder named 2014-09-22 and several files in a different parent folder in the identically-named folder from a different processing run.


In order to merge and retain duplicates using the Finder, you would have to open two Finder windows on those respective 2014-09-22 folders, and drag (move) or option+drag (copy) the files from the last created folder onto the open previous folder. The Finder will detect duplicate names and pop a dialog. If you keep the option key pressed after this dialog appears, then Keep Both will become a Skip button and your duplicate will not be placed in the destination folder — so if you are copying, release the option key:



In order to keep duplicate names, you click Keep Both (for each affected file) and the first duplicates will have the name IMG_0026 copy.CR2. If you repeat this merge process again from a different source folder, the first original duplicate's name is renamed to IMG_0026 2.CR2, and the third duplicate is named IMG_0026 3.CR2. If the contents of these matching names are different, that is on you, not Finder which only cares about the namespace collisions.


Organize files in folders on Mac - Apple Support

More on merging folders.




May 3, 2022 5:30 PM in response to VikingOSX

Hi @VikingOSX, do you know how to merge folders like you can with windows?

First batch of files we sorted into folders by date and worked as expected.

So then I uploaded a second large batch of 10,000 files to my 'sorting folder' and the application seemed to run and told me files were processed but when I checked in the date folders it only had the first batch of files that were sorted (not sure where the second batch was sorted to).

A complication is that some files may have the same name but I want to keep both.

So I created another sorting folder for the second batch and successfully ran the application creating date folders.

Now I want to merge the date folders, keeping the contents of both merged folders. Is this possible? Thanks



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.

Sort files into folders by date created using Automator

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