You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Sort files into folders by date created

It seems like this might be possible but I haven't found exactly what I'm looking for online... I have a ton of pictures and videos that I'd like to sort into folders based on the date the pictures or videos were taken. Can I automate the whole process of creating the folders named by date and then moving the files into the correct folder? I just discovered automator today and have no experience with apple script so I need specific directions. :)

iMac Line (2012 and Later)

Posted on Jan 10, 2019 8:44 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 13, 2019 4:58 AM

I have posted version 2.0 of the Ruby Script on my Google Drive Shared folder. It is nearly 40% faster than previous versions, for the same quantum of files.



I revised the file_created_date function to only run exiftool once, but look for DateTimeOriginal, CreateDate, and ModifyFileDate tags. Usually there are two found in Raw, and non-Raw camera .jpg files, and frequently identical, but just in case, I take the oldest date found. Other images will have the ModifyFileDate tag only — but no CreateDate. PDF have the ModifyFileDate, and CreateDate tags. As presently written, the script is ignoring PDF files.


Let me know if this solution is working for you, or if you have additional questions.

Similar questions

25 replies
Question marked as Top-ranking reply

Jan 13, 2019 4:58 AM in response to zarin123

I have posted version 2.0 of the Ruby Script on my Google Drive Shared folder. It is nearly 40% faster than previous versions, for the same quantum of files.



I revised the file_created_date function to only run exiftool once, but look for DateTimeOriginal, CreateDate, and ModifyFileDate tags. Usually there are two found in Raw, and non-Raw camera .jpg files, and frequently identical, but just in case, I take the oldest date found. Other images will have the ModifyFileDate tag only — but no CreateDate. PDF have the ModifyFileDate, and CreateDate tags. As presently written, the script is ignoring PDF files.


Let me know if this solution is working for you, or if you have additional questions.

Jan 10, 2019 4:43 PM in response to zarin123

I have a working Automator application that takes the input folder containing the image and video files that you want to process and creates a set of all files matching those extensions. Based on that data, it then checks the file creation date of each file, and if the date named folder does not exist, creates a new folder using the yyyy-mm-dd date format, and copies the respective file into it. Here is an example output when Automator completes:



I simply duplicated my Pictures folder to the Desktop, and let it rip. Two Automator actions:

  1. Ask for Finder Items
  2. Run Shell Script


I am using Ruby for the processing, and I believe the elapsed time for the file count suggests speed. Tested on macOS High Sierra with the System Ruby v2.3.7.p456. Testing performed on a 2011 Mac mini with 128GB SSD and 16GB RAM.


If you want the Ruby code and instructions to create the Automator application, let me know and I will post them here.


Some assumptions made:

  1. Date-stamped folders created in the same folder as the processed files
  2. All files that have the same creation date are placed in the appropriate yyyy-mm-dd folder
  3. The use of yyyy-mm-dd formatting for the folders as they sort well without white-space.
  4. That you would want process completion notification and counts.


A file date-stamped in the Finder will use the EXIF DateTimeOriginal field if it exists in the image, otherwise a UNIX creation date will be associated with the file.

Jan 13, 2019 9:38 AM in response to zarin123

The v2.0 application expects that you have installed exiftool at /usr/local/bin/exiftool. If it is not, then it will ignore using exiftool altogether, and just get the Ruby birthtime (System creation time as "2018-11-17 19:18:18" applied to the image that is shown in both fields of the Get Info window.


I just took a JPG from an iPhone X, made a copy of that file, and then changed the DateTimeOriginal, CreateDate, and FileModifyDate tags to reflect those fields in your Adobe Bridge screen capture above. So, here are my date fields:



And here is the result of passing that JPG file into v2.0 file_created_date function, where f is the test.jpg file.



If the function had not detected the /usr/local/bin/exiftool application, then it would have simply returned "2018-11-17" instead of the "2015-04-15" information, which I demonstrated separately as the birthtime result.


I suspect one of two things:

  1. exiftool is not installed in /usr/local/bin.
  2. The v2.0 script is not properly installed in Automator Run Shell Script action.





Jan 11, 2019 4:26 AM in response to VikingOSX

What additional image and video extensions do you need that are not shown in this initial set? What is not needed?


I need a few minutes yet this morning to test on Mojave 10.14.2, before posting. I found some code late last night that allowed me to pluck the DateTimeOriginal field from an image's EXIF data, but it is in a language that requires a compiler to use, and I would need additional time to translate it, and avoid the compiler necessity. This morning's post will not integrate the EXIF capability.


The current Ruby code only takes the items on the first level inside the prompted folder.

Jan 11, 2019 7:43 PM in response to VikingOSX

Thanks! I got it all set up and works great except for with the set of photos I have where the "date created" and the Exif "date time original" are different. (Example shown in image below.) I followed your instructions to get exiftool, and when it didn't sort those images, I tried the installation again in case I messed something up. It said exiftool 11.11 is already installed and up-to-date. Could I have done something else wrong?


Jan 12, 2019 1:35 PM in response to zarin123

All of my testing was on a larger directory of nearly 200 images. I just ran the v1.5 code from the Terminal on a folder containing a mixture of 20 images. I have the same version of exiftool installed as you. This was run on a 2011 Mac Mini with Core i5, 16GB RAM, and an internal SSD that is now 7+ years old. None of these were RAW images.

Jan 10, 2019 12:46 PM in response to zarin123

It is entirely possible to do, but there are a lot of details missing in your post that are required before a solution can be suggested.


For example, let's start with which date? Are you talking the date the picture was actually taken (which requires reading the metadata stored within the image file itself), or is the 'creation date' or 'modification date' of the file sufficient?


Secondly, what about granularity? Do you want to group pictures by each day? or all pictures from a specific month/year?


Let's start with that.

Jan 10, 2019 7:05 PM in response to zarin123

Currently, the Ruby application gets the creation date from the individual image. In general, Ruby does not include a standard library within it to access EXIF data in an image, and I do not post code that requires the extra confusion of adding these third-party modules. That said, there is a macOS native way to get EXIF data from an image that I am researching right now.


I can post the Ruby code and Automator steps first thing in the morning (Jan 11).

Jan 11, 2019 1:26 PM in response to zarin123

Had some issues with the hosting software that won't let me post the solution here. Getting a Google Drive shared folder and documentation ready. Will post the link here in a few minutes. I highly recommend having BBEdit or another language-aware programmer's editor installed. The Ruby script is zipped, and once unzipped, you need to open it in BBEdit so the formatting is preserved. Then you can safely copy/paste into the Automator Run Shell Script as I just did as part of testing.


Ruby solution now checks for the presence of Phil Harvey's exiftool, and will attempt to extract EXIF/IPTC creation date information with that tool. If the tool is not installed, or cannot extract creation date information, then the System creation date for the file will be returned as 'yyyy-mm-dd' and the folder created, if it does not already exist.


I also updated the code to build a set of all existing folders that match the 'yyyy-mm-dd' naming format, and only create new folders that are not in this set. This means that when you add files to this containing folder, and re-run the script, it may only need move the file into an existing 'yyyy-mm-dd' folder. Performance gain.

Jan 12, 2019 5:58 AM in response to zarin123

Earlier this morning, I updated the Ruby Script to v1.5 on the Google Drive location. Improved logic, and shaved about 13% off of the previous runtime for same quantum of files.


You don't need to run exiftool, unless you want too. The Ruby script will detect if it is installed, and will run it in the background — looking first for the tag 'DateTimeOriginal', and if not found, then the tag 'FileModifyDate' in the given image file. Whichever it finds, it will return a formatted date string as 'yyyy-mm-dd' that will be used for the folder name. If exiftool is not installed, or for some reason, it could not retrieve a date string value for either EXIF tag name, then it will simply get the System creation date from the file.


In the preceding, and in those cases where the given image does not have the tag 'DateTimeOriginal', the poorly named 'FileModifyDate' appears to be the actual creation date. Very misleading.


I have some image files, including raw file formats downloaded from the Internet, where the internal creation date, is different from the Finder Get Info created date. Expect that.


When you run the Automator application, what is the final dialog report on number of images, folders, and elapsed time?


Jan 12, 2019 1:24 PM in response to rccharles

I run osascript as a subprocess and provide it three data items on the command-line to generate the display dialog at processing end.


# for the AppleScript display dialog panel at processing end
ascript = <<~AS
  on run argv
    set filecnt to (item 1 of argv)
    set foldercnt to (item 2 of argv)
    set elapsed to (item 3 of argv)
    display dialog "Total files processed: " & filecnt & return & \
    "Total folders created: " & foldercnt & return & \
    "Processing time (s): " & elapsed with title "Processing Complete"
    return
  end run
AS

folders = Set.new
matched_files = Set.new
filecnt = 0
foldercnt = 0

elapsed = Benchmark.realtime do
  start_folder = File.expand_path(ARGV.first)

  Dir.chdir(start_folder) do
    Dir.glob("#{start_folder}/*.*").select do |f|
      matched_files << f if extensions.include?(File.extname(f.downcase))
    end

    # if previous run, collect existing folders matching yyyy-mm-dd
    Dir.glob("#{start_folder}/*").select do |f|
      z = File.basename(f)
      folders << z if valid_date?(z) && z.length == 10
    end

    matched_files.each do |f|
      # create_date = File.birthtime(f).strftime('%F') # yyyy-mm-dd
      create_date = file_created_date(f)
      FileUtils.mkdir(create_date, mode: 0o0755, verbose: false) \
                unless folders.include?(create_date)
      folders << create_date
      FileUtils.mv(File.basename(f), create_date, verbose: false)
    end
    filecnt = matched_files.count
    foldercnt = folders.count
  end
end
_ = Open3.capture2('osascript', '-e', ascript, filecnt.to_s,
                   foldercnt.to_s, elapsed.round(4).to_s)


Sort files into folders by date created

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