Flatten folder structure via AppleScript

Have you ever wanted to flatten a given folder’s structure? Flattening a folder means moving all the files from their various subfolders into one parent folder. You may want to do this to ease archiving, or to rearrange files that are stored in subfolders on a daily basis into a monthly wrap-up folder, for instance.

You can do this in the Finder, of course, but with a simple AppleScript, you can fully automate the process. The following script is indeed very simple—it won’t ask you which folder you want to flatten, nor can you control which files it will flatten. The script will run on the currently-selected folder in the Finder, and move every file it finds in every subfolder into a new folder located at the top level of the selected folder.

Here’s the code; enter this into Script Editor (in Applications -> AppleScript)

tell application "Finder"
  set theTopFolder to (selection as alias)
  set theFiles to a reference to every file of (entire contents of folder theTopFolder)
  set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files"}
  duplicate theFiles to theNewFolder
  --move theFiles to theNewFolder
end tell

Save the script somewhere in your user’s folder. To make it easy to use, drag it onto the Finder’s toolbar or sidebar, or into the Dock. To use it, select a folder in the Finder, then activate the script in the toolbar/sidebar/Dock. Depending on the size and number of files in the folder’s structure, this could take a while to run. When done, though, you’ll find a new Flattened Files folder sitting in the top level of the folder you selected. Within that will be all the files from all the subfolders. If there were duplicate filenames in some folders, all the files will be there, named with appended digits to make them unique.

Note that I’ve set the script up to copy the files, and not actually move them. I’ve included the code that moves the files instead; if you’re confident that the script works how you wish, and that you’ll never make a mistake, feel free to activate the move command instead of the duplicate command.

Before you do, though, realize that there is no “undo” for this script. Once you flatten a folder, it’s flattened for good. If you want to go ahead and move the files, with that warning in mind, just put -- in front of the duplicate line, and remove the -- from the move line. When run, the script will now move files, instead of copying them. But really, I recommend using duplicate and then manually deleting the original folder and files when you’re sure everything worked.

This really is a basic script—there’s no error checking, and there’s no recording of the original location of the moved files. However, it does the job, and leaves lots of room for customization if you’ve got some AppleScript skills. Thanks to Mac OS X Hints reader tedw for the basic script, and to an unnamed AppleScript wizard for helping me with some AppleScript syntax issues related to file and folder handling.

To comment on this article and other Macworld content, visit our Facebook page or our Twitter feed.
Shop Tech Products at Amazon