Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to move files from subfolders level up with AppleScript or Automator?

Hi Everyone,


I'm new to AppleScript as well as to Automator.


I have following issue.


Before starting I'd like to give an example the root of the main folder as well as subfolders


mainFolder/Subfolder1/Subfolder1.1/file1

mainFolder/Subfolder1/Subfolder1.1/file2

mainFolder/Subfolder1/Subfolder1.2/file1


mainFolder/Subfolder2/Subfolder2.1/file1

mainFolder/Subfolder2/Subfolder2.2/file1

mainFolder/Subfolder2/Subfolder2.2/file2


I need to move files from within Subfolders1.1, 1.2, to Subfolder1 , Subfolders2.1, 2.2 to Subfolder2 and than delete Subfolders1.1, 1.2 and Subfolders2.1, 2.2

I found number of AppleScripts as well as Automator workflow to move files from Subfolders to main directory but not to parent subfolder.


Is there any way to automate this task?


Thanks

OS X Mountain Lion (10.8)

Posted on Dec 4, 2012 4:42 PM

Reply
6 replies

Dec 6, 2012 9:37 AM in response to ludwip

Hi,


If you use the Niel's script, replace every item by every file, otherwise the folders will be included in the list.



Here's another way to do this :


set x to choose folder
tell application "System Events"
      repeat with p in (get path of folders of x)
            set this_Folder to p as alias
            move (files of folders of this_Folder whose visible is true) to this_Folder
            delete folders of this_Folder
      end repeat
end tell

Dec 6, 2012 11:54 PM in response to Niel

Hi Niel,


First of all thank you for your help.


I was trying to run a script but following error message occur:



"error "Finder got an error: Can’t get folder \"Subfolder1.1\" of folder \"Subfolder1\" of folder \"Main folder\" of folder \"Desktop\" of folder \"ludwip\" of folder \"Users\" of startup disk." number -1728 fromfolder "Subfolder1.1" offolder "Subfolder1" offolder "Subolder test" offolder "Desktop" offolder "Ludwik" offolder "Users" ofstartup disk



Instead of moving contents of Subfolders 1.1 and 1.2 to Subfolder1 and delete them afterwards AppleScript moves entire Subfolders 1.1 and 1.2 into Subfolder1. All the contents remain in Subfolders 1.1 and 1.2.

Also is there any chance for the script to repeat itself in every Subfolder1, Subfolder2, Subfolder3 etc. instead of me copying the script to each of the Subfolders?"


Thanks again for your help,

Dec 7, 2012 12:07 AM in response to Jacques Rioux

Hi Jacques Rioux,


I've tried your script but it doesn't seem to work for me at all.

Maybe I've done something wrong?

Thanks



Jacques Rioux wrote:


Hi,


If you use the Niel's script, replace every item by every file, otherwise the folders will be included in the list.



Here's another way to do this :


set x to choose folder tell application "System Events"       repeat with p in (get path of folders of x)             set this_Folder to p as alias             move (files of folders of this_Folder whose visible is true) to this_Folder             delete folders of this_Folder       end repeat end tell

Dec 7, 2012 6:36 AM in response to ludwip

Hi ludwip,



ludwip wrote:


Script works fine for the first few folders and than following error comes up:

error "System Events got an error: AppleEvent timed out." number -1712



If you get this error, there must be a lot of files to move at the same time.

You should use the "with timeout" statement, because the command fails to complete in the allotted time (the default is two minutes).



set x to choose folder
tell application "System Events"
      with timeout of 0 seconds -- no AppleEvent timed out 
            repeat with p in (get path of folders of x)
                  set this_Folder to p as alias
                  move (files of folders of this_Folder whose visible is true) to this_Folder
                  delete folders of this_Folder
            end repeat
      end timeout
end tell


Also, you must consider that the script does not check names, an error will occur if the script tries to move two files with the same name.

How to move files from subfolders level up with AppleScript or Automator?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.