Applescript: Copy all contents of a folder into every folder in a directory - applescript

I'm in a bit of a bind and an Applescript noob. I've been stumbling through a script that would allow me to copy the contents of (TemplateFolder) into all of the client folders in a directory, with the added crux that all of the existing clients' folder contents be moved into a folder within the client labeled "Old Files" or something of the sort. I'll include images with my script thus far to illustrate what I'm trying to achieve. Thank you everyone in advance for all your help.
P.S. Since I don't have the rep required, I'll have to post links to my images.
http://imgur.com/a/lL9q7
The first image is the template folder (the folder I'd like all contents copied into each client folder).
The second image is an example of an existing client folder with all the bad structure (or lack thereof).
The final image is the expected results where the template folder's contents are moved into the client folder and the original content of the client's folder are moved into a separate folder titled "Old Structure Files".
Below is the applescript I've written, with help from others, to copy the contents. However there are missing components and some elements that need to change; currently the applescript simply copies the entire folder rather than just the contents and it makes a simple copy rather than inserting, the script is not recursive for an entire directory, and there's no function to move the existing client files into a "Old Structure Files" folder. Again, any and all help is greatly appreciated :)
on run
set source_folder to choose folder with prompt "Select folder to be duplicated:" as string
my do_main_script(source_folder)
end run
on open of source_folder_list
repeat with i from 1 to number of items in the source_folder_list
set this_folder_path to item i of the source_folder_list as string
if last character of this_folder_path is ":" then
my do_main_script(this_folder_path)
end if
end repeat
end open
on do_main_script(source_folder)
tell application "Finder" to set source_folder to folder (source_folder)
tell application "Finder" to set the target_folder to (parent of source_folder)
if source_folder is not "" and target_folder is not "" then
set new_folder_name to (name of source_folder as string) & " duplicate"
set source_folder to source_folder as string
set target_folder to target_folder as string
my create_new_folder(target_folder, new_folder_name)
my duplicate_folder_structure(source_folder, target_folder & new_folder_name & ":")
end if
end do_main_script

In the script bellow, the template folder is at fixed place on desktop, all the content of the customer selected folder is moved to a new folder "old structure", and after that, all content of the template folder is duplicated into the customer folder. If I well understand, this is what you're looking for :
set Templatefolder to "HD:Users:my_user:Desktop:Template"
set CustomerFolder to (choose folder with prompt "Select customer folder to be re-organised")
tell application "Finder"
set CustomerData to every item of CustomerFolder
set OldF to make new folder in CustomerFolder with properties {name:"Old Strutucture Files"}
move CustomerData to OldF
set Newstructure to every item of folder Templatefolder
duplicate Newstructure to CustomerFolder
end tell

Related

Automatically move files of a certain type from folder A to B whenever they are added to folder A on macOS

Using Automator, I created a "Folder Action" to move files of certain types (i.e. "Kind IS Movie") from folder "FolderA" to folder "FolderB" as soon as they are added to folder "FolderA". It works fine except that it doesn't move files from subfolders (e.g. doesn't work if the movie files are added to "FolderA/SubA/"), and apparently there's no option on Automator to make it work on the subfolders.
Does anyone have any idea about how to either change that on Automator or how could I create a script for that? Thanks!
This following AppleScript folder action code should give you a general idea of how to accomplish what you are looking for. When a file or folder is added to a folder that this folder action is attached to, it will search all folders and sub folders for files (I used QuickTime movie as the kind of file... But you can change that to whatever you want) then those files will be moved to the folder that you will define in property moveToFolder
You can save this following code in Script Editor.app directly into your /Users/Your_User_Name/Library/Workflows/Applications/Folder Actions folder, as a .scpt file. Once the script is saved into that folder, it will be available to attach to any folder as a folder action using the Folder Actions Setup.
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Kind Of File To Act Upon
property fileKind : "QuickTime movie"
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose kind is fileKind)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to
OR Here Is A Version Which Defines The Name Extensions Of The Files To Act Upon
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Name Extensions Of Files To Act Upon
property videoFileExtensions : {"m4v", "mkv", "avi", "mp4", "mov", "wmv", "mpg"}
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose name extension is in videoFileExtensions)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to

AppleScript - Move all the files contained in subfolder to a top folder

I am looking for an edit to the current script. What I need is moving all the files from subfolders (recursively) in to one top folder, however if a file with the same name exist, create a new top-folder and continue there, this is what I have so far:
tell application "Finder"
try
set Random_name to random number from 100 to 9999
set theTopFolder to (choose folder)
set theFiles to a reference to every file of (entire contents of folder theTopFolder)
set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files"}
move theFiles to theNewFolder
on error
set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files" & Random_name}
move theFiles to theNewFolder
end try
end tell
Just to be clear the structure of the path is not:
Mainfolder/subfolder/file.xxx but Mainfolder/subfolder/sulbfolder2/subfolder3/....100/file.xxx so the script needs to work recursively which it does but it stops when a file exist with the same name
When a file with the same name exist my edit creates a new folder with Flattened Files+random number however when another file with the same name is moved the script stops for an error instead going ahead and creating a new Flattened Files+randonnumber folder. Any ideas?
Thank you
Your script isn't using recursion, it just lets the Finder get all the files. The additional error you are getting is because you are trying to move the whole list of files again.
One solution would be to step through the file items, testing for duplicates as you go, and make new folders as needed. The following script just moves duplicates to added folders (note that an error will still stop the script). I don't know how you are sorting the file list, so I included a line to uncomment to continue moving file items to the added folder.
set theTopFolder to (choose folder)
tell application "Finder"
set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files"}
set theFiles to every file of (entire contents of folder theTopFolder) as alias list
repeat with aFile in theFiles
if file ((theNewFolder as text) & (name of aFile)) exists then -- use added folders for duplicates
set counter to 1
set done to false
repeat until done
set suffix to text -2 thru -1 of ("000000" & counter) -- leading zeros for sorting
set alternateFolder to (theTopFolder as text) & "Flattened Files" & space & suffix
tell me to (do shell script "mkdir -p " & quoted form of POSIX path of alternateFolder) -- make new folder as needed
if file (alternateFolder & ":" & (name of aFile)) exists then -- continue to next one
set counter to counter + 1
else
move aFile to folder alternateFolder
# set theNewFolder to folder alternateFolder -- uncomment to continue moving here after a duplicate
set done to true
end if
end repeat
else
move aFile to folder (theNewFolder as text)
end if
end repeat
end tell

Copy folder structure and merge sub directories

enter image description hereI'm not sure where best to post this, hopefully someone can advise if this isn't right, I've been a long time browser but this is my first post. Anyway..
I need to automate copying a load of folders to a new location and merging sub directories.
I will use ABC as an example text string which represents the company name and is a constant value.
The current structure is like:
2012 Jobs
ABC12 001-099
ABC12-001
jpgs
raw
web
tiffs
ABC12-002
ABC12-003
...
ABC12 100-199
ABC12 200-299
2013 Jobs
...
2014 Jobs
...
What I need to do is copy this structure to a new location, but merge the jpgs and web folders in to one named jpgs for example and the raw and tiffs in to another folder named tiffs.
I created an AppleScript to create the folder structure but these are obviously all just blank folders, would be much easier to just copy what is there already and start again, open to any suggestions, you're the experts! Thanks
Image shows structure before and how I want it after:
Before and After
The script bellow does partially what you need (I think !).
It looks for every "raw" in the top folder you selected (i.e. the folder containing all the "2012 jobs", 2013 Jobs",...)
For each "raw" folder found, it looks if, at same level a "tiffs" folder already exists (if not it creates it).
Then it moves all the files from "raw" to the "tiffs". Finally, it deletes the empty "raw".
set TopFolder to choose folder with prompt "Select parent folder to process"
tell application "Finder"
set RawsFolder to every folder of entire contents of TopFolder whose (name is "raw")
repeat with aFolder in RawsFolder
set ParentFolder to container of aFolder -- get upper folder
set TiffFolder to (ParentFolder as string) & "tiffs"
if not (exists TiffFolder) then make new folder in ParentFolder with properties {name:"tiffs"}
set FilesRaw to every file of aFolder --get all files from raw folder
move FilesRaw to folder TiffFolder -- move them to tiffs folder
delete aFolder -- delete the raw folder
end repeat
end tell
To complete, you must do same thing again, but with "web" folders to be copied into "jpgs". You have 2 ways to do that :
1) move the "tell Finder" block into sub routine with 2 strings parameters (raw, tiffs), adjusting the script to replace each occurrence of raw/tiffs by the parameter. then you call this routine with ("raw", "tiffs") and again with ("web","jpgs")
2) you just copy/paste the tell finder block and change "raw"/"tiffs" manually to "web" / "jpgs"
Up to you...
Warning : don't forget to run this on a full copie of your parent folder !

AppleScript : How to get files in folder without hidden files?

I actually have two questions.
How to exclude hidden files like .DS_STORE, Icon when I try to get files in folder ?
I've tried "without invisibles" but it seems not working.
How to set my var the_new_folder as an existing folder if already exists ?
Thanks for answers.
My code:
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
tell application "Finder"
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
end tell
on file_to_folder(the_folder)
tell application "Finder"
-- HELP NEEDED HERE
-- HOW TO EXCLUDE HIDDEN FILES (Like Icon, .DS_STORE, etc)
set the_files to files of the_folder
repeat with the_file in the_files
-- Exclude folder in selection
if kind of the_file is not "Folder" then
set the_path to container of the_file
set the_file_ext to name extension of the_file
-- Remove extension of the file name
set the_file_name to name of the_file as string
set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name
-- Make the new folder with the file name
try
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
on error
-- HELP NEEDED HERE
-- HOW TO SET the_new_folder AS THE EXISTING FOLDER
end try
-- Move the file in the new folder
move the_file to the_new_folder
end if
end repeat
end tell
end file_to_folder
tell application "Finder"
(display dialog ("It's done!") buttons {"Perfect!"})
end tell
Using the System Events context instead of Finder:
bypasses the problem with the AppleShowAllFiles preference[1]
is much faster in general.
Using the visible property of file / folder objects in the System Events context allows you to predictably determine either all items, including hidden ones (by default), or only the visible ones (with whose visible is true):
# Sample input path.
set the_path to POSIX path of (path to home folder)
tell application "System Events"
set allVisibleFiles to files of folder the_path whose visible is true
end tell
Simply omit whose visible is true to include hidden files too.
The code for either referencing a preexisting folder or creating it on demand is essentially the same as in the Finder context:
# Sample input path.
set the_path to POSIX path of (path to home folder)
# Sample subfolder name
set the_subfolder_name to "subfolder"
tell application "System Events"
if folder (the_path & the_subfolder_name) exists then
set subfolder to folder (the_path & the_subfolder_name)
else
set subfolder to make new folder at folder the_path ¬
with properties {name: the_subfolder_name}
end if
end tell
[1] In order to predictably exclude hidden items, a Finder-based solution is not only cumbersome but has massive side effects:
You need to determine the current state of the the AppleShowAllFiles preference (defaults read com.apple.Finder AppleShowAllFiles),
then turn it off.
then kill Finder to make the change take effect (it restarts automatically) - this will be visually disruptive
then, after your code has run, restore it to its previous value.
then kill Finder again so that the restored value takes effect again.
I believe that your first question is not a problem. Your code works fine for me.
As for the second issue, the simplest method is to use the text representation of the_path, and simply build the new folder, and see if it already exists:
set the_path_Text to (the_path as text)
set try_Path to the_path_Text & the_file_name
if (folder try_Path) exists then
set the_new_folder to (folder try_Path)
else
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
end if
If you are truly having difficulty with the first code section, please post a new question with more details, such as a copy of the Result section of the Script.
Thank you to all of you ! #mklement0 #craig-smith
You will find below the corrected code with your help!
If I share this code, you want to be cited for thanks?
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
-- Get user folder
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
on file_to_folder(the_folder)
tell application "System Events"
-- Get all files without hidden
set the_files to files of the_folder whose visible is true
repeat with the_file in the_files
-- Remove extension of the file name
set the_file_ext to name extension of the_file
set the_file_name to name of the_file as string
set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name
-- Make a new folder or get the existing
set the_path to POSIX path of the_folder
if folder (the_path & the_file_name) exists then
set the_new_folder to folder (the_path & the_file_name)
else
set the_new_folder to make new folder at folder the_path with properties {name:the_file_name}
end if
-- Move the file to the new folder
move the_file to the_new_folder
end repeat
end tell
end file_to_folder
-- Ending dialog
display dialog ("It's done!") buttons {"Perfect!"}

create subfolder in subfolder and move file

i am new to this and am looking for help: i have a piece of code that is working but wanna extend it and can't make it work so far:
i have this to create a project folder in a chosen location, with two folders and two subfolders:
`set JobName to text returned of (display dialog "Please enter Project Name:" default answer "New_Project")
set loc to choose folder "Choose Parent Folder Location"
tell application "Finder"
set newfo to make new folder at loc with properties {name:JobName}
set targetFolder to make new folder at newfo with properties {name:"01_folder"}
make new folder at targetFolder with properties {name:"01_sub1"}
set targetFolder to make new folder at newfo with properties {name:"02_folder"}
make new folder at targetFolder with properties {name:"02_sub1"}
end tell`
i have two questions:
1. how can I create a subfolder in subfolder {name:"01_sub1"}?
2. how can I move a file into one of the folder that are being created?
(I know how to move to a fixed path but not one that is being chosen in the process of the same script)
i would be super thankful for any hints!
spoke
You are already doing it in your other lines so I don't understand why you have problems. You have this line:
make new folder at targetFolder with properties {name:"01_sub1"}
Just change that to capture the folder variable like this...
set subFolder1 to make new folder at targetFolder with properties {name:"01_sub1"}
Then you can put a sub folder into that with...
set anotherSubFolder to make new folder at subFolder1 with properties {name:"another subfolder"}
Note you'll also capture that folder variable so all you need to do to move a file to it is...
move someFile to anotherSubFolder
All I'm showing you is using the same logic you've already used elsewhere in the code.

Resources