Copy files and folders with subfolders - Please help!

Please help. I have been trying to write this script all day, and keep runiing into errors. All I am trying to do is copy/duplicate a filestructure from one spot to another on the same G4. Well, actually, I want to do other things, like set up a criteria for which files I want to move over, but that can be done later. Right now, i just want to take one folder, which in itself would have files, and several levels of subfolders holding their own files, and copy that over to another location on the same hard drive - maintaining all the same file structure (like you would do by manually dragging and dropping with your mouse).

Thanks in advance for all your help!

This will let you chose a source folder and copy it into a new folder on the desktop called ‘Destination.’

set the_folder to choose folder
set the_path to path to desktop as string
tell application “Finder”
make initial destination folder on Desktop
make new folder at desktop with properties {name:“Destination”}
create the full destination path
set destination_ to the_path & “Destination” as alias
copy the files
duplicate contents of the_folder to destination_
end tell

this script was automatically tagged for
color coded syntax by Script to Markup Code
written by Jonathan Nathan


You said that you wanted to eventually only process certain items. You can do this many ways depending on what you want to do. here’s an example using a criteria that the Finder knows:

set main_folder to (choose folder)
set list_of_jpegs to ProcessFolder(main_folder)

– recursive subroutine
on ProcessFolder(the_folder)
– do something with every file that matches a certain criteria
– for example a listing of all jpegs
set jpeg_list to {}
tell application “Finder”
set jpeg_list to (name of every file of the_folder whose file type is “JPEG”)
on error – there are no files of this type
– do nothing
end try
set folder_list to (every folder of the_folder)
end tell
repeat with this_folder in folder_list
set jpeg_list to jpeg_list & ProcessFolder(this_folder)
end repeat
return jpeg_list
end ProcessFolder

Sometimes you cannot use the Finder like this with the filter refernce and you may need to repeat through every file. It depends what you are trying to do.

BTW, most if not all recursive subroutines can me made linear. I made a similar subroutine to this linear but the recursive one is faster and in this case easier to understand I think.


Thanks for responding so fast people! This has really given me some new ideas for what I’m trying to do.

Kel, you’re input was pretty close to what my end-result could be. But instead of just listing files, like you do with JPEGS in your example, I want to duplicate just the files whose modification date’s are later than a certain date i’ll set up as a variable. Do you know how I can alter that script of yours to do that? I tried a couple of different approaches, but came up with different errors.

Say like…
–create var for earliest date of file
set archiveDate to date “Tuesday, April 20, 2004 12:00:00 AM”

–then use this in the if statement
if…modification date of file comes after (archiveDate) then…
–copy the files
duplicate (but keeping folder/subfolder structure)

Is that clear?

Thanks for all your help, and please let me know if you have any suggestions!



This is the type of situation where you may not be able to use the filter reference form in the Finder because of bugs depending on your system or I don’t know if they fixed it yet in Panther. On my system it’s better not to use the Finder but a repeat loop. Or maybe it’s a bug in the System Events beta. If you’re using Panther, then maybe somebody else can show you an easier way. Are you running Panther?

Keeping the folder structure is not too hard, but It’s the bug that is making me change the script.



Here’s an example. I used the current date and checked if the modification dates of items were less than the current date. So you have to change that. I quickly did it as I’m getting hungry and it may need checking, but I added some error checking. Just an example. It will take a long time for big lists and would need a turbo boost in this case.

Remember, this goes through every item in the folder so there may be shortcuts if you’re using Panther:

set main_folder to (choose folder)
set desk_ref to (path to desktop)
set archive_date to (current date)
ProcessFolder(main_folder, desk_ref, archive_date)

on ProcessFolder(the_folder, the_container, archive_date)
– make a copy of the_folder at the_container
tell application “Finder”
– get the name
set folder_name to (name of the_folder)
– make the folder
if (exists folder folder_name of the_container) then
set folder_name to (folder_name & " copy")
end if
set folder_copy to (make folder at the_container with properties {name:folder_name})
on error – the copy already exists
error “Error: move or delete the copy!”
end try
end tell
set item_names to (list folder the_folder without invisibles)
– eliminate the “.” files if any
repeat with this_name in item_names
if this_name begins with “.” then
set item_names to rest of item_names
exit repeat
end if
end repeat
– process the items
repeat with this_name in item_names
set folder_path to the_folder as string
set item_path to (folder_path & this_name)
set item_info to (info for (item_path as alias))
if (folder of item_info) then
set item_ref to (item_path & “:”) as alias – add the colon
ProcessFolder(item_ref, folder_copy, archive_date)
else if (modification date of item_info) < archive_date then
set item_ref to item_path as alias
tell application “Finder” to duplicate item_ref to folder_copy
end if
end repeat
end ProcessFolder


Wow, wow, wow!

Thank you! This is absolutely perfect! I have to admit, I’m sort of new to Applescript. I know JavaScript and Actionscript pretty well, but there are some differences with those languages. I had it going pretty well, but couldn’t get that last 10%… thanks for all your help!