Sunday, June 16, 2019

#1 2019-06-08 11:09:45 pm

moosmahna
Member
Registered: 2015-06-03
Posts: 22

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:

Applescript:

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

Offline

 

#2 2019-06-08 11:25:16 pm

moosmahna
Member
Registered: 2015-06-03
Posts: 22

Re: Run script in Subfolders of Subfolders

I tried this, but it did not work:

Applescript:


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

Offline

 

#3 2019-06-09 04:50:12 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3458

Re: Run script in Subfolders of Subfolders

Finder is unable to treat the instruction:

Applescript:

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.

Applescript:

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

Offline

 

#4 2019-06-09 04:54:59 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 145

Re: Run script in Subfolders of Subfolders

One level of subfolders deeper:

Applescript:


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

Last edited by KniazidisR (2019-06-09 04:59:35 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#5 2019-06-09 09:32:33 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11563
Website

Re: Run script in Subfolders of Subfolders

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

Applescript:

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 & " \\;"


regards

Stefan

Offline

 

#6 2019-06-09 01:47:15 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 145

Re: Run script in Subfolders of Subfolders

StefanK wrote:

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

Applescript:

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.

Last edited by KniazidisR (2019-06-09 01:48:22 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#7 2019-06-09 02:26:29 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11563
Website

Re: Run script in Subfolders of Subfolders

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


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)