Copy files in nested subfolders into one destination

Posted this in Automator, but maybe an AppleScript is better?

Is there a way to select a series of folders that all have a nested hierarchy, and dump just the files into a destination?

Thanks! Would make my workflow a lot easier.

Hi,

this is a minimal version. There is no error handling and existing files with the same name will be overwritten.
The files will be moved, not copied


set sourceFolder to POSIX path of (choose folder with prompt "Choose source folder")
set destinationFolder to POSIX path of (choose folder with prompt "Choose destination folder")
do shell script "/usr/bin/find " & quoted form of sourceFolder & " -type f ! -name '.*' -print -exec /bin/mv {} " & quoted form of destinationFolder & " \\;"

Cool. Is there a way to make it copy instead of move? I don’t really know shell stuff - can I replace with /bin/cp or something?

Weird - changed it to cp and it works, but when I copy a folder tree with 10 files, it only copies 7. If I delete the files in the destination (or just 1 file), the others appear??? Doesn’t matter if I refresh, only deleting seems to do it.

Is there a way to pass an argument to the AS like with Autmator where you can choose Get Selected Finder Items or something like that?

if you use cp you must add the flag -r to copy also subfolders recursively.

Get Selected Finder Items is equivalent to


tell application "Finder"
	set sel to selection
end tell

sel contains the selected items as Finder file specifiers

Ah, thank you. The recursive switch fixed that.

How do I pass the selected argument to the rest of the script?

Something like set sourceFolder to sel? For something like this, is it any better making it into an Automator workflow so I can right-click, or using a droppedItem thing - not that I can do that either :slight_smile: - and making it an Application bundle and putting it on my dock? Any difference in speed, etc?

I don’t like Automator, because it’s just a GUI for AppleScript.
Everything you can do in Automator, you can also do in AppleScript.
But it doesn’t work vice versa :wink:

Assuming the selected Finder items are all folders


tell application "Finder" to set sel to selection

set destinationFolder to POSIX path of (choose folder with prompt "Choose destination folder")
repeat with i in sel
	do shell script "/usr/bin/find " & quoted form of POSIX path of (i as alias) & " -type f ! -name '.*' -print -exec /bin/cp -r {} " & quoted form of destinationFolder & " \\;"
end repeat

Funny, just said that to a friend that it seemed like Automator was just a GUI for AppleScript for those of us that don’t really know how to script. :slight_smile:

Thank you so much - this is perfect. Added it to my AppleScript menu and it will definitely streamline my workflow.

Please find the one which i use to do the above mentioned process for just moving the files of its every sub folder to the main folder that is choose and delete the empty folders in it.


tell application "Finder"
	set yy to choose folder with prompt "Choose base folder"
	set xx to name of every file of entire contents of folder yy
	move every file of entire contents of folder yy to folder yy
	my deleteemptyfolder()
end tell

on deleteemptyfolder()
	tell application "Finder"
		try
			set yy to choose folder with prompt "Choose base folder"
			set folders_list to folders of entire contents of folder yy
			set nbr to (count folders_list)
			if nbr = 0 then set nbr to 1
		on error err number numX
			if numX = -1728 then return -- 0 folders
			error err
		end try
		
		repeat with i from nbr to 1 by -1
			set This_folder to item i of folders_list
			if items of This_folder is {} then delete This_folder
		end repeat
	end tell
end deleteemptyfolder

I have not used shell command is this script.
Please correct me if i am wrong anywhere, or else use this if you need this.

Thanks & Regards
appmani

appmani, I tried your all-AppleScript version on my Mac and I’m afraid it didn’t work–it gave me a time-out error and crashed the Finder.