Skip navigation

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

930 Views 6 Replies Latest reply: Dec 7, 2012 6:36 AM by Jacques Rioux RSS
ludwip Calculating status...
Currently Being Moderated
Dec 4, 2012 4:42 PM

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)
  • Niel Level 10 Level 10 (234,640 points)

    Open subfolder1 and run a script such as:

     

    tell application "Finder"

    repeat with this_item in (get every item of entire contents of (folder of window 1))

    move this_item to parent of parent of this_item

    end repeat

    end tell

     

    sort the window by kind, and delete the folders.

     

    (72389)

  • Jacques Rioux Level 4 Level 4 (1,925 points)

    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
    
  • Jacques Rioux Level 4 Level 4 (1,925 points)

    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.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.