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.

Relocating files based on creation date

Hello,

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.

Thanks.

MacBook Pro, Mac OS X (10.5.7)

Posted on Aug 23, 2009 7:28 AM

Reply
74 replies

Sep 13, 2014 4:50 PM in response to red_menace

Thank you for your very long history of responses to this thread and all the little "tweak" requests... and yes, i have another:) I am only two weeks into mac ownership (quite a learning curve) and I am using Automator and your Apple script to address what I see as a functionality gap within iPhoto and/or Image Capture - The ability to customize file names when importing.


Here is my general workflow:

A -

1. Import photos into folder

2. rename photos based on date taken (YYYY_MM_DD_HHMMSS)

3. move photos into folders according to (YYYY-MM-MONTH)- 2014 - 09 - September (the 09 is the month not the day)


B -

1. Take selected photos and add prefix name to the end of the prefix (e.g. 2014_09_13_181232 - Jr.'s 1st Birthday)


C -

1. Import all my properly dated subfolders into iPhoto

2. move to external backup drive.

3. Delete from mac

I understand that iPhoto doesn't really showcase the file name once its in the db, but this method helps me in 3 ways 1) iPhoto uses the folder structure and the prefix info to help auto - organize (events and titles) upon import without me having to redo much, 2) if I export/email/upload a photo it will be named properly 3) If i ever decide to switch photo management tools i will have some sort of hope rebuilding an unmanaged folder structure with good file names instead of a whole bunch of IMG 001s.


I have the Automator workflow about 95% figured out and I think i can get the rest of the way there with some trial and error. However hoping you could help with the folder name tweak I have described in A-3 above which uses a combo of the two get date examples you illustrated - by using both month format types (numerical and text). Also, I would prefer to use the date taken rather than date created. This is usually not an issue because they are the same when imported from the camera/iPhone, but this would help use the same code for files that were received some other way and may have changed the original date created.


Thanks again for your good work.

Sep 17, 2014 1:05 PM in response to red_menace

Hello,


I think that this might be what I'm looking for, but if not, I'm wondering if you might be able to help me.


I've recently discovered Automator, but I can't figure out how to get it to do what I need it to do. My pictures are saved in folders by the date they were created. In updating to new downloading software several times over several years, the format of the dates is not the same, thus they are no longer in chronological order in my larger "Photos" folder. I tried re-naming the folders through automator based on date created, but it pulls the date the folder was created, NOT the date the pictures were created, which is not accurate. How do I tell automator to look at the content of the folders and to either re-name them or create new folders based on the creation date of the pictures? Or will this script work (with modification) to accomplish this task?

My pictures are already organized, so really, what I want to do is just re-name the folders to a uniform yyyy_mm_dd format. Can you help?

Many, many thanks in advance!

Dec 7, 2014 7:31 AM in response to kashlan

I'm not great at applescript. I successfully used this script to organize my entire library about a month back. I downloaded pictures from my camera today and wanted to organize them so I ran the script again and started getting an error "number -1721". I removed the expression {input, parameters} and was able to get past the first error but then ended up with a second error "The variable input is not defined." number -2753 from "input". Any thoughts on what changed and why this suddenly doesn't want to work any more?

Apr 19, 2015 1:49 AM in response to red_menace

Thanks Red_Menace for your thoroughness. It's been quite helpful. This script works while in Automator but as I am not familiar with AppleScript, I have not been able to get this script to work as an external application. Could you show me and where in the main body of the script to set the input variable with the proper code syntax. Also, could you do the same for replacing the alternate button "Cancel" terminology with buttons {"Cancel", "OK"} in the showSkippedAlert handler. Thanks.


castrohouse

Apr 19, 2015 5:43 AM in response to kashlan

Ok I figured out how to use this "AppleScript". I will experiment and document more with video later.


How To Use this script "Relocating Files Based On Creation Date"


Thanks to Red_Menance...


i. Run Automater

ii. Search for these items and drag into window to the right


  1. Ask for FInder Items {Type: Folders} -- can also get files; (Change Type to Folders, Check Multiple Selection)
  2. Get Folder Contents (be careful with getting subfolders - this action will go into packages)
  3. Run AppleScript -- paste in the original script (minus the HTML) or the various tweaks posted


4. Click Run!

Nov 30, 2015 7:55 PM in response to kashlan

My wife gave me an SD card with 1500 pictures on it at 10:30 pm and wanted them sorted immediately for a project due by midnight. I stumbled across this thread and successfully got red_menace's code to work, but I really don't like Automater. And I'm really comfortable with AppleScript. Moreover, having the Finder do the moving in El Capitan is slow and inefficient—the Finder calls the sound handler to move each and every file, so you also get a silly clinking sound for every file (1500 times in my case). I decided to build my own AppleScript-only script that uses the Bourne shell to create folders and move files, so it's fast and quiet. It can be run from Script Editor or saved as a dropplet—drop a folder on it and it will sort its contents; double click it and it lets you select the folder to be sorted. It has no output; it just gives up on the files it can't move for any reason.


-- Begin script


on run

set SelectedFolder to choose folder

SortFiles(SelectedFolder)

end run


on open (DroppedFolder)

SortFiles(DroppedFolder)

end open


on SortFiles(SortFolder)

set SortFolder to SortFolder as alias

try

tell application "Finder" to set SortFolderContents to entire contents of SortFolder

on error

quit

end try

set FilesToProcess to {}

set FolderMakeList to {}

tell application "Finder"

repeat with ThisItem in SortFolderContents

if class of ThisItem is not folder then -- Only process files

set ThisFile to POSIX path of (ThisItem as alias)

set ThisFilesFolder to POSIX path of SortFolder & text 1 thru 10 of (creation date of ThisItem as «class isot» as string) & "/"

set FilesToProcess to FilesToProcess & {{ThisFile, ThisFilesFolder}}

if ThisFilesFolder is not in FolderMakeList then set FolderMakeList to FolderMakeList & ThisFilesFolder

end if

end repeat

end tell

repeat with ThisFolder in FolderMakeList

set ScriptCode to "mkdir '" & ThisFolder & "'"

try

do shell script ScriptCode

end try

end repeat

repeat with ThisFile in FilesToProcess

set ScriptCode to "mv '" & (item 1 of ThisFile as string) & "' '" & (item 2 of ThisFile as string) & "'"

try

do shell script ScriptCode

end try

end repeat

end SortFiles


-- End Script

Dec 2, 2015 5:22 PM in response to diablote

It turns out the Finder entire contents command is really slow, too—doing a find shell command is about 10 times faster. I also experimented with stat to see if I could eek out more performance on getting the creation date, but it turned out the Finder was about 25% faster than the shell. I trimmed out as much as I could from the rest of the script to maximize speed; the result is below.


-- Begin script


on run

SortFiles(POSIX path of (choose folder))

end run


on open (DroppedFolder)

set DroppedFolder to POSIX path of DroppedFolder

if text (length of text of DroppedFolder) of DroppedFolder is not "/" then quit

SortFiles(DroppedFolder)

end open


on SortFiles(SortFolder)

set AppleScript's text item delimiters to return

set SortFolderContents to the text items of (do shell script "find '" & SortFolder & "' -type f")

set FilesToProcess to {}

set FolderMakeList to {}

repeat with ThisItem in SortFolderContents

set ThisFile to ThisItem as string

if ThisFile does not contain "/." then

tell application "Finder" to set ThisFilesFolder to SortFolder & text 1 thru 10 of ((creation date of ((POSIX file ThisFile) as alias)) as «class isot» as string) & "/"

set FilesToProcess to FilesToProcess & {{ThisFile, ThisFilesFolder}}

if ThisFilesFolder is not in FolderMakeList then set FolderMakeList to FolderMakeList & ThisFilesFolder

end if

end repeat

repeat with ThisFolder in FolderMakeList

try

do shell script ("mkdir '" & ThisFolder & "'")

end try

end repeat

repeat with ThisFile in FilesToProcess

try

do shell script ("mv '" & (item 1 of ThisFile) & "' '" & (item 2 of ThisFile) & "'")

end try

end repeat

end SortFiles


-- End script

Dec 3, 2015 5:11 PM in response to diablote

Love the script! efficient ... however...


Can you tweak it so the dialog window doesn't pop up requesting a destination folder?


My idea is to create a folder action whereby files dropped on the folder will automatically be sent to the correct date folder without having to pick a start or destination folder. I have recurring files sent to me that are automatically renamed and sorted before being shared with my accountant


so the script would be called upon each time a file was dropped on the folder to place the file in a separate year folder and ideally month sub-folder


thanks for the feedback!


Christian

Dec 4, 2015 10:52 AM in response to Christian Nørgaard

It's a fairly straightforward change to make it sort by YYYY/YYYY-MM. Making it a folder action is either simple but inefficient or complicated and possibly efficient. I erred toward simple, but whenever you add something to the folder the script will enumerate the entire folder contents (not just the added items), and every file will get processed (including the ones that were already in the correct subfolders). The outcome is the same, but if the folder holds a ton of files it'll probably take a while just to process one file.


To use the script as a folder action, save the script below as a script (not app) and put it in the /Library/Scripts/Folder Action Scripts/ folder. Right-click on the folder you want auto-sorted and select the script from the list.


-- Begin script


on adding folder items to ThisFolder after receiving AddedItems

SortFiles(POSIX path of (ThisFolder))

end adding folder items to


on SortFiles(SortFolder)

set AppleScript's text item delimiters to return

set SortFolderContents to the text items of (do shell script "find '" & SortFolder & "' -type f")

set FolderMakeList to {}

repeat with ThisItem in SortFolderContents

set ThisFile to ThisItem as string

if ThisFile does not contain "/." then

tell application "Finder"

set DateString to text 1 thru 7 of ((creation date of ((POSIX file ThisFile) as alias)) as «class isot» as string)

set ThisFilesFolder to SortFolder & text 1 thru 4 of DateString & "/"

set ThisFilesSubfolder to ThisFilesFolder & text 1 thru 7 of DateString & "/"

end tell

if ThisFilesFolder is not in FolderMakeList then

try

do shell script ("mkdir '" & ThisFilesFolder & "'")

end try

set FolderMakeList to FolderMakeList & ThisFilesFolder

end if

if ThisFilesSubfolder is not in FolderMakeList then

try

do shell script ("mkdir '" & ThisFilesSubfolder & "'")

end try

set FolderMakeList to FolderMakeList & ThisFilesSubfolder

end if

try

do shell script ("mv '" & ThisFile & "' '" & ThisFilesSubfolder & "'")

end try

end if

end repeat

return FolderMakeList

end SortFiles


-- End script

Dec 7, 2015 6:19 AM in response to diablote

Wow thanks for the edit!


To clarify, the current folder structure has a parent "accounting" folder in which there would be yearly subfolders (2013, 2014, etc), each of which would have monthly subfolders.


The document to be filed would be deposited in "accounting and thereafter automatically moved to the correct /Accounting/year/month subfolder


If all this stands, my main question is: does the script scan "accounting " AND subfolders (thereby RE sorting already sorted files) or does it only process documents in the first folder level (i.e. /accounting/ )and NOT in the subsequent sub folders (/year/ and /year/month/) not that this really matters(or?) if there aren't too many files to sort

Dec 7, 2015 3:34 PM in response to Christian Nørgaard

The line


set SortFolderContents to the text items of (do shell script "find '" & SortFolder & "' -type f")


will list the full file contents of the folder SortFolder, so it would indeed resort the entire /Accounting/ directory. You could merge the first and last scripts to use the Finder instead; instead of calling the entire contents command you could call the files in command.

Relocating files based on creation date

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