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

Aug 14, 2017 4:04 AM in response to KinFPV

Your problem, hardware or software, may not be exactly the same as that of the original poster of this older thread, and it can be very confusing for everybody if we try to answer more than one question in each thread, which can also result in you applying the wrong advice to your particular problem.


In order for us to give your problem our proper attention to try to solve it, would you kindly start your own thread, describing the trouble you are having in the fullest detail, including completing your details to show what Mac or iDevice you are using, what operating system, and what version of the application in question. Please remember to post in the forum relevant to your hardware or version of OS X. A full list of all the support forums is here:


http://discussions.apple.com/index.jspa

Jun 27, 2017 11:35 AM in response to diablote

Brilliant.


I have been working with this script to sort into folders named month and year: (JUN-2011, OCT-2015)


--BEGIN SCRIPT

tell application "Finder" set mifold to choose folder set filelist to every file of mifold as list

set foldlist to every folder of mifold as list


repeat with i from 1 to number of items in filelist


set subj to item i of filelist as alias

set epoch to (do shell script "stat -f \"%B\" " & (quoted form of (POSIX path of subj)))

set caldate to do shell script ("date -r " & epoch & " \"+%b-%Y\"")

if not (exists folder caldate of mifold) then make new folder at mifold with properties {name:caldate}

end if

try (* try ignores errors from locked files *)

move file subj to folder caldate of mifold end try end repeat repeat with j from 1 to number of items in foldlist

set subj to item j of foldlist as alias

if name of subj does not start with "200" then set epoch to (do shell script "stat -f \"%B\" " & (quoted form of (POSIX path of subj)))


set caldate to do shell script ("date -r " & epoch & " \"+%b-%Y\"")

if not (exists folder caldate of mifold) then make new folder at mifold with properties {name:caldate}

end if

try (* try ignores errors from locked items *) move folder subj to folder caldate of mifold

end try

end if

end repeat

end tell


--SCRIPT END


Problem is speed. Moves one file at a time and doesn't matter if I run in automator or as script.

any suggestions on how to speed this up or modify your script to do just month and year?


t

Jun 28, 2017 2:05 AM in response to robpriceincalifornia

Since I posted the script, I've made it even leaner. For your desired sort, I simply replaced the line


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) & "/"


with your epoch method.


Cheers.


-- 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 FolderMakeList to {}

repeat with ThisItem in SortFolderContents

set ThisFile to ThisItem as string

if ThisFile does not contain "/." then

set ThisFilesEpoch to do shell script "stat -f \"%B\" '" & ThisFile & "'"

set ThisFilesFolder to SortFolder & (do shell script ("date -r " & ThisFilesEpoch & " \"+%b-%Y\"")) & "/"

if ThisFilesFolder is not in FolderMakeList then

try

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

end try

set FolderMakeList to FolderMakeList & ThisFilesFolder

end if

try

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

end try

end if

end repeat

end SortFiles


-- End Script

Jun 28, 2017 3:26 AM in response to robpriceincalifornia

A bit more on speed…


You can never tell which will be faster, a Finder command or a shell command. You can instrument your script with a handy millisecond function to record elapsed time between points in your script:


on milliDT() -- This function generates the current system time seconds with millisecond precision.

try

do shell script "defaults read b" -- This creates a harmless error with a time stamp down to milliseconds.

on error errorText

text 18 thru 23 of errorText -- This will parse out the seconds down to the millisecond.

end try

end milliDT


With it I found that generating your Mmm-YYYY format with the the shell epoch and date commands takes 5-10x longer than parsing a Finder date command (which produces the same result):


tell application "Finder" to set FinderDate to (creation date of ((POSIX file ThisFile) as alias))

set ThisFilesFolder to SortFolder & (text 1 thru 3 of ((month of FinderDate) as string) & "-" & year of FinderDate)


Cheers.

Aug 14, 2017 3:46 AM in response to diablote

Hey everyone, thank you very much for the new work, I would like to ask you :

- How can I make the date looks like YYYYMMDD (for exemple : "20170814") ?

I tried to remove the "-" in the script and didn't work :

set TheDate to text 1 thru 8 of (creation date of AnItem as «class isot» as string) -- YYYYMMDD

try --

- How can I make the actions more simple ? :

1 - SELECT the files I want to sort

2 - Right click -> "SORT the files into folders by dates" -> The script sort where the files are

3 - Option : Possibility to CANCEL the entire script action (important…)


Can't wait for your answer 😉…


Here is the code how I have it now :

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 8 of (creation date of AnItem as «class isot» as string) -- YYYYMMDD

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

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:"
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
</pre>

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

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.

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.