5
votes

I have been trying to make a script to move every file within a folder to the root folder to include every sub folder. I don't want to make a new folder just move it to the root folder.

I want to be able to select the folder and then the action be completed on that specific folder only.

The reason is for organization, my exact situation is I have over a TB of movies and there's files within folders within folders. I want all those files on the root folder so that I can organize them how I deem fit. Also I would like to copy and delete instead of move as research has led me to believe is the best route.

Well I couldn't get the script to work but i did find a link to a workflow that does exactly what I'm looking for:

http://www.macworld.com/article/1160660/automator_filesfromsubfolders.html


I updated above keeping this section for history purposes, everything in between this dotted lines is what I would have deleted after editing

I have tried to reverse engineer this script below, that i found but, I just can't get it to work like I want to. Any and all help will be much apprieciated, I am very new to scripting and I have come to a rock.

2nd Attempt tell application "finder" set main_folder to (choose folder with prompt "choose the main folder") 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 try end try end tell

1st Attempt tell application "Finder" set sourceFolder to folder "MOVE ME" -- of disk "blah", etc. my moveFilesFrom(sourceFolder) end tell

on moveFilesFrom(thisFolder) tell application "Finder" set filesToMove to every file of thisFolder repeat with aFIle in filesToMove move aFIle to folder destFolder end repeat set subFolders to (every folder of thisFolder) repeat with aFolder in subFolders my moveFilesFrom(aFolder) end repeat end tell end moveFilesFrom

The finder times out when I use it on a a folder containing a lot of information and sub-folders, I used it on a test folder yesterday and it worked with no problem but substantially smaller.

I am now using this script provided by adayzdone

    set myFolder to (choose folder with prompt "choose the main folder")
    tell application "Finder"
    set myfiles to get every item of (entire contents of folder myFolder) whose kind ≠ "Folder"
    move myfiles to myFolder
    end tell

3

3 Answers

1
votes

You can try something like this:

set myFolder to (path to desktop as text) & "testFolder"
tell application "Finder"
    set myFiles to get every item of (entire contents of folder myFolder) whose kind ≠ "Folder"
    move myFiles to myFolder
end tell

or you can use a shell script like Lri suggested:

set myFolder to quoted form of (choose folder with prompt "choose the main folder")'s POSIX path
do shell script "cd " & myFolder & "; find . -type f -exec mv {} " & myFolder & " \\;"
1
votes

A quick recursive subroutine will handle this:

set theFolder to POSIX path of (choose folder with prompt "Choose a folder")

tell application "System Events"
    set theFiles to my recursiveSearch(folder theFolder)
    move theFiles to folder theFolder
end tell

on recursiveSearch(theFolder)
    tell application "System Events"
        set theFiles to every file of theFolder whose visible is true
        set theSubFolders to every folder of theFolder
        repeat with thisFolder in theSubFolders
            set theFiles to theFiles & my recursiveSearch(thisFolder)
        end repeat
        return theFiles
    end tell
end recursiveSearch

This is easy to modify if you want to accomplish other things (e.g., deleting the empty subfolders, moving only specific kinds of files, etc). Let me know if it needs tweaking.

0
votes

Folders have an entire contents attribute:

tell application "Finder"
    set f to choose folder
    move files of entire contents of f to f
end tell

You could also use find in the shell:

cd ~/Movies/Folder/; find . -type f -exec echo mv '{}' . \;