Relocating files based on creation date
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
15 replies
Sign in or join Beachball Help to reply.
- 0
Klaus1 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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:
- 0
robpriceincalifornia ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
- 0
diablote ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
- 0
robpriceincalifornia ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Such an old post, I didn't expect a reply. I modified your line:
ThisFilesFolder to SortFolder & text 1 thru 10
To
ThisFilesFolder to SortFolder & text 1 thru 7
Outputs Folders with year and month (2017-07)
Nice to have EPOCH version to customize short month.
Cheers.
- 0
diablote ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
- 0
KinFPV ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
- 0
red_menace ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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 folderif 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 trytell 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 repeatShowSkippedAlert for SkippedItems
return the output -- pass the result(s) to the next action
end runto 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> - 0
kashlan ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Thanks a TON for your help with this request. It worked like a charm and flawlessly. I sorted 1000+ video clips that were all in one folder exactly as desired.
I hope others get to find your post. Bet there are many others with a similar need.
Deeply appreciated!!!!
- 0
jono1515 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Sweet, I've been wishing I knew how to put something like this together for awhile now, saved me a lot of work. Thanks.
- 0
RavenMac ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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).
- 0
rock4Him ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I'm guessing you could replace the line that starts with:
set TheDate to text 1 thru 10 of ...
to
set TheDate to text 1 thru 4 of
and that would give you folders based on the year only.
- 0
mehighpenet ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Thank you soo much for this post... saved me a ton of time!
- 0
objetty ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
- 0
MrTcGill ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Brilliant script. Thanks so much :-)
Give your self a well deserved pat on the back.
- 0
red_menace ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.