Run script in Subfolders of Subfolders

Hy everyone,
I found a script that copys files from the subfolders. It works perfect. But now I tried to modify the script that it runs one level of subfolders deeper.

Here is the script:

set main_folder to (choose folder with prompt "Choose the main folder")
tell application "Finder"
	set sub_folders to folders of main_folder
	repeat with each_folder in sub_folders
		move every file of each_folder to main_folder with replacing
	end repeat
end tell

This works if you have a main folder and one subfolder. But I have a main folder, then a subfolder and again subfolder in this subfolder.

Like:
Main Folder
Subfolder1
Subfolders2
Subfolders2
Subfolder1
Subfolders2
Subfolders2

This Script runs with Subfolder1. Can you please help me modify the script to select the Main Folder, and copy the files from the Subfolders2 in the parent subfolder1?

Thanks a lot
Kind Regards

P.S. Sorry for my english

I tried this, but it did not work:


set main_folder to (choose folder with prompt "Choose the main folder")
tell application "Finder"
	set sub_folders to folders of main_folder
	set sub_FolderDeep to folders of sub_folders
	repeat with each_folder in sub_FolderDeep
		move every file of each_folder to sub_folders with replacing
	end repeat
end tel

Finder is unable to treat the instruction:

  set sub_FolderDeep to folders of sub_folders

because sub_folders is a list of folders.
The code below really scan the subfolders but as you may see, I disabled the code supposed to move the files.
I let as an exercise the creation of the code moving the files correctly.
In your code the Finder is asked to move a file in a list of folders which it can’t do.

set main_folder to (choose folder with prompt "Choose the main folder")

tell application "Finder"
	set sub_folders to folders of main_folder as alias list
	repeat with a_sub_folder in sub_folders
		set sub_FolderDeep to folders of a_sub_folder as alias list
		if sub_FolderDeep is not {} then
			(*
	repeat with each_folder in sub_FolderDeep
		--move every file of each_folder to sub_folders with replacing
	end repeat
	*)
		end if
	end repeat
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 9 juin 2019 12:50:00

One level of subfolders deeper:


set main_folder to (choose folder with prompt "Choose the main folder")

tell application "Finder"
	set sub_folders to folders of main_folder
	repeat with each_folder in sub_folders
		move every file of each_folder to main_folder with replacing
		set sub_folders_2 to folders of each_folder
		repeat with each_folder_2 in sub_folders_2
			move every file of each_folder_2 to main_folder with replacing
		end repeat
	end repeat
end tell

A more versatile and faster solution is a combination of the find and mv command of the shell

set maxdepth to 2

set main_folder to POSIX path of (choose folder with prompt "Choose the main folder")
do shell script "find " & quoted form of main_folder & " -maxdepth " & (maxdepth as text) & " -type f ! -name '.*' -print -exec /bin/mv {} " & quoted form of main_folder & " \\;"

Yes, I have repeatedly regretted that I did not know the secrets of the shell command line well enough. Your solution is more versatile and faster.

Actually it’s no secret.

  • -maxdepth specifies the number of levels
  • -type f considers only files
  • ! -name ‘.*’ excludes (invisible) files starting with a dot
  • print -exec hands over the result to another shell command
  • bin/mv {} destination \; moves the result of the find expression to destination

Nice and elegant script/tool !!!
Thanks!