Skip navigation

Relocating files based on creation date

11388 Views 55 Replies Latest reply: Feb 6, 2014 4:41 AM by Mike9586 RSS
  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 24, 2013 2:55 PM (in response to biggerboy)

    Sure - the script is set up so that you can put in a specific destination folder (it will ask if it isn't set or isn't a valid path), but it will use the containing folder if you set theContainer variable (near the beginning of the script) to missing value, i.e.

     

      set theContainer to missing value

  • biggerboy Calculating status...
    Currently Being Moderated
    Feb 24, 2013 7:45 PM (in response to red_menace)

    Works like a charm. Thanks.

  • rdarton Calculating status...
    Currently Being Moderated
    Jun 11, 2013 7:30 AM (in response to red_menace)

    red_menace, THANK YOU for taking the time to put this up and to reply to so many people's questions!

     

    I'm very green when it comes to Automator and AppleScript. They intimidate me. I was able to use your instructions and this is ALMOST doing what I'd like it to do.

     

    Could you tell me what I need to change if I want don't want to choose the folders where the files are originating from and where they are going to every time?

     

    For example, I will have a folder "A" and whenever files are dropped into folder "A", I want them to automatically get moved and organized into date folders within folder "B" without any prompts. Is this possible? If so, please let me know how to do it!

     

    Thank you!!!

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Jun 11, 2013 8:13 PM (in response to rdarton)

    You can create a Folder Action workflow, which will get the items added to the folder, and set a specific destination path in TheContainer variable, for example:

        set TheContainer to "Macintosh HD:Users:you:someParentFolder:someFolder:"

     

    You can use a single choose folder statement in the AppleScript Editor and look at the result log to see what your particular path should look like - if you use an invalid path, the script will ask you to choose the folder.

  • tavernau Calculating status...
    Currently Being Moderated
    Jul 27, 2013 8:07 AM (in response to kashlan)

    Absolute Gold.

     

    Saved me a lot of time. Thanks very much. 21000+ files sorted while I drank a cup of tea.

  • Old Man George Calculating status...
    Currently Being Moderated
    Oct 14, 2013 9:19 AM (in response to kashlan)

    Would someboy mind reposting the orginal code without the messed up forum formatting to the commented lines. I'm no expert at AppleScript and I'm having a hard time sorting out the comments from the code. Much appreciated. This will be great if I can get it working.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Oct 14, 2013 10:03 AM (in response to Old Man George)

    Give this a try.  AppleScript comments are delimited by -- or # for an end-of-line, and (* *) for multiple lines.

    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:"
        else
          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}
        tell application "System Events" to if button returned of (display alert alertText message (theMessage & return & skippedItems) buttons {"Cancel", "OK"}) is "Cancel" then error number -128
      end if
      return
    end showSkippedAlert
  • objetty Calculating status...
    Currently Being Moderated
    Oct 14, 2013 10:25 AM (in response to Old Man George)

    Below is a copy/paste directly from my AppleScript editor.  Looks like it might be the same as Old Man George's script but I didn't check line for line.  Worked great for me.  Copy/paste:

     

     

    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) -- YY_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:"

                        else

                                  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

              return

    end ShowSkippedAlert

  • biggerboy Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 2, 2013 10:40 AM (in response to kashlan)

    I had this script hooked up to Image Capture, but now it doesn't seem to work with Mavericks. Has anyone else run into this?

  • rebelliousspirit Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 3, 2013 6:57 AM (in response to red_menace)

    Amazing amazing!!  Was looking for this for so long!  Thank you so much!

     

    Is there any way to create folders in YYYY-MMM format?  i.e. - 2013-DEC

     

    That would be awesome.

     

    Thansk once again for your contribtion.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Dec 5, 2013 7:25 PM (in response to rebelliousspirit)

    You can change the statement that sets theDate to somethng like:

    tell (get creation date of anItem) to set theDate to (its year as text) & "-" & text 1 thru 3 of (its month as text)



  • svenreed Calculating status...
    Currently Being Moderated
    Dec 8, 2013 10:39 AM (in response to red_menace)

    Thanks for this red_menace! Saved me tons and tons of time!!

  • cheekydog Calculating status...
    Currently Being Moderated
    Dec 8, 2013 11:23 AM (in response to red_menace)

    red_menace-- thanks so much for this script. I'm new to Automator and AppleScript. In fact, yours is the first I've used and it was only this week. Very useful, so thanks again.

     

    Wanted to share some extra info with folks...

     

    My case is that I had a bunch of photos on discs. Using an old Win 2k system (where I have a disc drive as my MacBook Air doesn't) to copy files over to my new Synology NAS.

     

    When one does so, the files are (a) in a locked state and (b) use the date that you copied them as a Created date rather than the date they were actually created. The Created date is actually reflected in the Modified date. (Side note: if one copies a file locally (e.g., to my MacBook Air), then the Created date is restored correctly.)

     

    Modified your script slightly to use the Modified date rather than the create date. That was easy. (theDate = modDate). Next, I found this script to unlock all files in a directory:

     

    set fileSample to choose folder with prompt "Choose a folder to process."

    set folderName to name of (info for fileSample)

    tell application "Finder" to set theFiles to files of folder fileSample

     

    tell application "Finder"

              repeat with oneFile in theFiles

                        set lockedStatus to locked of oneFile

                        if locked of oneFile is true then

                                  set locked of oneFile to false

                        end if

              end repeat

    end tell

     

    Run this first and problem (a) solved. Then run your script (slightly modifed), and problem (b) solved.

     

    There's probably a way I can do this in onl fell swoop (in a single Automator job) and also probaly a way for me to change the create date of a file to the modifed date (to correct this little issue). If there's any input from anyone on either of these, would be greatly appreciated. 

     

    If not, no big deal. For now, this is good enough as it gets the files in the right directlry, and my time is tight to get this job done.

     

     

     

     

     

     

     



     


    MacBook Air, OS X Mountain Lion (10.8.5), Autompater applescript c
  • rebelliousspirit Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 8, 2013 11:44 AM (in response to red_menace)

    Worked like a charm.  Thanks so much.

     

    Though I'm bit of a dummy with Applescript and I am trying to not having to choose the folder I am moving to and set it to my Pictures folder.  I tried the following many times...

     

    set theContainer to "/Users/Dharma/Pictures" -- a Finder path to a destination folder, or missing value for the source folder

     

    It still asks me to choose the folder.  What am I doing wrong?

     

    Regards

    Dharma

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Dec 8, 2013 12:18 PM (in response to rebelliousspirit)

    The Finder does not like POSIX paths - you will need to use a Finder/HFS path, or coerce it, for example:

     

    set theContainer to POSIX file "/Users/Dharma/Pictures" as text

Actions

More Like This

  • Retrieving data ...

Bookmarked By (3)

Legend

  • 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.