Skip navigation

Relocating files based on creation date

11408 Views 55 Replies Latest reply: Feb 6, 2014 4:41 AM by Mike9586 RSS
1 2 3 4 Previous Next
kashlan Calculating status...
Currently Being Moderated
Aug 23, 2009 7:28 AM

I have a folder full of hundreds of Video clips. I am interested in a script that will

1. look at the files sequentially
2. For each file read the creation date
3. If a Folder with that creation date does not exist, create a folder with that creation date (YYYY-MM-DD)
4. Move the file to the folder with the file's creation date

I've never used automator. Is this easy to do using that program?

The reason I want to do this is to avoid having to manually re-organize the videos by date, if I were to import the contents of the existing folder of video clips into iMovie.

Appreciate any input you can provide.

MacBook Pro, Mac OS X (10.5.7)
  • red_menace Calculating status...
    Currently Being Moderated
    Aug 23, 2009 9:17 AM (in response to kashlan)
    Automator is designed to take small, pre-written routines (actions) and string them together into a workflow. There isn't a default action to do something like this, but the Run AppleScript action can be used. Actually, an AppleScript would probably be the way to go, but I just happened to have an Automator action lying around for some reason.

    Tested workflow:
    1) *Ask for FInder Items* {Type: Folders}
    2) *Get Folder Contents* (be careful with getting subfolders - this action will go into packages)
    3) *Run AppleScript:*
    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    font-weight: normal;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px; height: 340px;
    color: #000000;
    background-color: #FFEE80;
    overflow: auto;"
    title="this text can be pasted into an Automator 'Run AppleScript' action">
    on run {input, parameters} -- create folders and move
    make new folders from file creation dates (if needed), then move document files into their respective new folders
    if no container is specified (missing value), the new folder will be created in the containing folder of the item
    if the container is not a valid path (and not missing value), one will be asked for
    input: a list of Finder items (aliases) to move
    output: a list of the Finder items (aliases) moved

    set output to {}
    set SkippedItems to {} -- this will be a list of skipped items (errors)
    set TheContainer to "" -- a Finder path to a destination folder, or missing value for the source folder

    if TheContainer is not missing value then try -- check the destination path
    TheContainer as alias
    on error
    set TheContainer to (choose folder with prompt "Where do you want to move the items?")
    end try

    tell application "Finder" to repeat with AnItem in the input -- step through each item in the input
    if TheContainer is not missing value then -- move to the specified folder
    set {class:TheClass, name:TheName, name extension:TheExtension} to item AnItem
    else -- move to the source folder
    set {class:TheClass, name:TheName, name extension:TheExtension, container:TheContainer} to item AnItem
    end if
    if TheClass is document file then try -- just documents
    set TheDate to text 1 thru 10 of (creation date of AnItem as «class isot» as string) -- YYYY-MM-DD
    try -- check if the target folder exists
    get ("" & TheContainer & TheDate) as alias
    on error -- make a new folder
    make new folder at TheContainer with properties {name:TheDate}
    end try
    -- duplicate AnItem to the result
    move AnItem to the result
    set the end of output to (result as alias) -- the new file alias
    on error -- permissions, etc
    -- set the end of SkippedItems to (AnItem as text) -- the full path
    set the end of SkippedItems to TheName -- just the name
    end try
    end repeat

    ShowSkippedAlert for SkippedItems
    return the output -- pass the result(s) to the next action
    end run

    to ShowSkippedAlert for SkippedItems
    show an alert dialog for any items skipped, with the option to cancel the workflow
    parameters - SkippedItems [list]: the items skipped
    returns nothing
    if SkippedItems is not {} then
    set {AlertText, TheCount} to {"Error with AppleScript action", count SkippedItems}
    if TheCount is greater than 1 then
    set theMessage to (TheCount as text) & space & " items were skipped:"
    set theMessage to "1 " & " item was skipped:"
    end if
    set {TempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
    set {SkippedItems, AppleScript's text item delimiters} to {SkippedItems as text, TempTID}
    if button returned of (display alert AlertText message (theMessage & return & SkippedItems) ¬
    alternate button "Cancel" default button "OK") is "Cancel" then error number -128
    end if
    end ShowSkippedAlert
    MacBook Pro, Mac OS X (10.5.8), /  G4 Digital Audio, G4 mini [Tiger 10.4.11]  /  G3 MiniTower [OS 9.2]
  • jono1515 Calculating status...
    Currently Being Moderated
    Mar 31, 2011 12:52 PM (in response to kashlan)
    Sweet, I've been wishing I knew how to put something like this together for awhile now, saved me a lot of work. Thanks.
  • RavenMac Calculating status...
    Currently Being Moderated
    Apr 14, 2011 8:17 AM (in response to jono1515)
    This is fantastic! However, if I needed to modify the code to make folders on "year" only and place all images based on that, where would the change need to be made? (forgive the question, AppleScript is still rather new to me).
    Mac Pro, Mac OS X (10.6.5)
  • rock4Him Calculating status...
    Currently Being Moderated
    Aug 23, 2011 6:13 PM (in response to RavenMac)

    I'm guessing you could replace the line that starts with:


    set TheDate to text 1 thru 10 of ...




    set TheDate to text 1 thru 4 of


    and that would give you folders based on the year only.

  • mehighpenet Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 26, 2011 10:38 AM (in response to red_menace)

    Thank you soo much for this post... saved me a ton of time!

  • objetty Calculating status...
    Currently Being Moderated
    Jan 20, 2012 9:57 PM (in response to red_menace)

    I'm glad I came across your post.  I did a lot of searching before I found something that will help me and this looks like it will.  I am new to Mac and helpless with AppleScript.  I have succesfully created some Automator actions for renaming files, etc. 


    My goal is to take many files in one folder and have them placed into folders based on the "Date Created" field with that date being the name of the folder in the format of 2012_month_day (month and day as two digits).  For example today is 2012_01_20.


    I've tried cutting and pasting the information in your post to a blank AppleScript but get error after error when I try to run the script.


    Would you be so kind as to give me more basic instructions in how to turn your post into a valid AppleScript?


    Thanks in advance.


    iMac OS X (10.5.8),  3.06 GHz Intel Core 2 Duo,  4GB 1067 MHz DDR3

  • MrTcGill Calculating status...
    Currently Being Moderated
    Feb 10, 2012 3:58 PM (in response to red_menace)

    Brilliant script. Thanks so much :-)

    Give your self a well deserved pat on the back.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 10, 2012 4:28 PM (in response to objetty)

    oops, didn't see your post.  The script part is between the <pre ...> and </pre> tags (the text formatting shows up in posts predating the current forums), so if that part is being included the Script Editor won't know what it is.  If you are wanting to use it as a script outside of Automator, just get rid of the {input, parameters} part in the run handler, and set the input variable by using something like choose file with multiple selections allowed.  You will also need to replace the alternate button "Cancel" terminology with buttons {"Cancel", "OK"} in the showSkippedAlert handler, since that term only works in Automator.

  • Amien90 Calculating status...
    Currently Being Moderated
    Jan 1, 2013 11:51 AM (in response to red_menace)

    Thanks fot this script. Searched along time for something like this .. but it doesnt quite work. Might be a OS issue.


    The script works, but all pictures are moved to 2013-01-01 instead of the creation date. any idea?


    i think it's in this part : «class isot»

  • Amien90 Level 1 Level 1 (0 points)
    Currently Being Moderated
    Jan 1, 2013 12:50 PM (in response to Amien90)

    checked again .. seems like the YYYY-MM-DD goes ok, but it gets the current date instead of the date of file creation

  • biggerboy Calculating status...
    Currently Being Moderated
    Feb 24, 2013 10:17 AM (in response to red_menace)

    How can this be modified to work as an Image Capture plug-in? Would love to have the photos downloaded to the Pictures folder already organized into folders....

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 24, 2013 11:08 AM (in response to biggerboy)

    You should be able to just use the Run AppleScript action in an Image Capture Plugin document - the plugin automatically receives the files, so it is not necessary to use the Ask for Finder Items or Get Folder Contents actions to get the input items.

  • biggerboy Level 1 Level 1 (0 points)
    Currently Being Moderated
    Feb 24, 2013 2:36 PM (in response to red_menace)

    It worked! Thanks. One last question - possible to just have the pictures go in sorted in the same folder they're in (Pictures) instead of having to select the folder? If not, then I can live with it.



1 2 3 4 Previous Next


More Like This

  • Retrieving data ...

Bookmarked By (3)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.