Skip navigation

Relocating files based on creation date

11382 Views 55 Replies Latest reply: Feb 6, 2014 4:41 AM by Mike9586 RSS
  • rebelliousspirit Calculating status...
    Currently Being Moderated
    Dec 8, 2013 4:28 PM (in response to red_menace)

    Thank for this.

     

    Only after a lot of error receiving I realised it required additional / at the end of the path.  So the following worked in my case. 

     

    Full of thankfulness! 

     

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

  • biggerboy Calculating status...
    Currently Being Moderated
    Dec 8, 2013 8:13 PM (in response to rebelliousspirit)

    Does the tilde work when setting theContainer? e.g.

     

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

     

    Would that work?

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

    You can't depend on tilde expansion, but for standard locations you can use the path to command from Standard Additions, for example:

     

    set theContainer to (path to pictures folder) as text

  • biggerboy Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 8, 2013 8:32 PM (in response to red_menace)

    Thanks.

  • NickAvallone Calculating status...
    Currently Being Moderated
    Dec 24, 2013 12:27 PM (in response to red_menace)

    This script was running beautifully prior to Mavericks, but now it doesn't work for me at all. Here's what I have, pulled from Automator:

     

    -----------------------------------

     

    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

     

     

     

    --------------------------------------------

    The above is set to  Folder Action for a specific folder. When I move files into the designated folder, nothing happens, despite this script being set as a Folder Action in Finder. When I run it in Automator it happily reports that the workflow is completed. But nothing actually happens in Finder -- the files haven't moved or anything.

     

    I have no script expertise at all (obviously) but was so grateful to have found this script back when it was working. Any help would be appreciated.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Dec 24, 2013 12:40 PM (in response to NickAvallone)

    The script still works for me in Mavericks - what is the rest of your workflow?

  • NickAvallone Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 24, 2013 1:46 PM (in response to red_menace)

    HI Red: Thanks for helping.

    It's just Folder Action > Run AppleScript, and the script it's running is exactly as above. I specified a particular folder (named IMPORT SORTER) to run the workflow on, and that's it.

     

  • mike4802 Calculating status...
    Currently Being Moderated
    Dec 28, 2013 11:36 AM (in response to kashlan)

    Hi all -

    If anyone isn't comfortable with AppleScript, another way is to leverage the existing exiftool executable. After you incorporate this magic into your automator workflow, it will be immune to OS updates since we will be running exiftool as a script.

     

    Big thanks to Phil Harvey for making this such a powertool in the world of thousands of image files.

    http://www.sno.phy.queensu.ca/~phil/exiftool/

     

    To see the tool in action, insert your SD card on your Mac, open a command terminal and :

    $ cd /Volumes/[your_cardname]

    $ exiftool -o . -r -P '-directory<datetimeoriginal' -d ~/Pictures/%Y/%m .

     

    What this little gem will do is recursively copy all the images from an SD card into a folder structure under ~/Pictures/2013/12 or whatever the exif data reveals for the image(s).  If you want more/less granularity, change the %Y/%m to be whatever you need.  

     

    Go here for details on the date format:  http://owl.phy.queensu.ca/~phil/exiftool/filename.html

     

    Hope someone enjoys this as much as I did.

  • Linc1100 Calculating status...
    Currently Being Moderated
    Jan 5, 2014 2:11 AM (in response to NickAvallone)

    Can anyone give me the script to sort the files into year/month instead of year/month/day

     

    BTW, the script is awesome

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Jan 5, 2014 6:19 AM (in response to Linc1100)

    You just need to drop a few characters from the date string, for example:

     

        set theDate to text 1 thru 7 of (creation date of AnItem as «class isot» as string) -- YYYY-MM

  • Linc1100 Level 1 Level 1 (0 points)
    Currently Being Moderated
    Jan 5, 2014 1:46 PM (in response to red_menace)

    Thanks heaps! Is there a way to change the script to only move pictures and videos as i have old backups of computers and they have documents and PDF files that i want to move separately

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Jan 5, 2014 3:19 PM (in response to Linc1100)

    The Run AppleScript action just moves items passed to it to folders based on a date.  You can use some of the included Automator actions, such as Filter Finder Items, to filter the list of files before passing them on to the script.

  • Linc1100 Level 1 Level 1 (0 points)
    Currently Being Moderated
    Jan 9, 2014 11:39 PM (in response to red_menace)

    I have tried that but it just comes up with no photos or videos when i know there is lots of them in there

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Jan 10, 2014 4:58 AM (in response to Linc1100)

    The Filter Finder Items action uses Spotlight, so the items would need to be in a location that gets indexed.

  • Mike Maas Calculating status...
    Currently Being Moderated
    Jan 26, 2014 2:06 PM (in response to kashlan)

    The system I use is a folder for each year then subfolders for the full creation dates under the year folder.  The files are then stored in the subfolder of the date the file was created.  I can get it to create both a year folder and a creation date folder but the creation date folder is another folder in the The Container not as a subfolder of the year folder.  How can I get it to work?

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.