Use Automator to copy multiple folders with different amounts of folders while removing parent folder
To make sure The problem statement is clear, let’s assume:
- You have a number of folders named using the format
123x456
, e.g.300x250
,728x90
, etc. These are all contained in a single, top-level folder with some arbitrary name, e.g.01_FILE_NUMBER
(unimportant); - Each folder named like
123x456
contained a single folder calledfolderA
, and nothing else; - Each folder called
folderA
contains a single folder calledfolderB
, and nothing else; - Each folder called
folderB
contains a single folder named identically to the123x456
second-tier folder that contains this branch of the directory sub-tree, e.g.728x90
; - Finally, these bottom-tier folders contain a
.PNG
image file whose filename is identical to the parent folder, e.g.728x90.png
; - Summarising all of this: all
.PNG
image files have a path of the form01_FILE_NUMBER/728x90/folderA/folderB/728x90/728x90.png
. Importantly,folderA
has no siblings, andfolderB
has no siblings, i.e. they reside in their parent directory by themselves.
Assuming this is accurate...
I would do the bulk of the file and folder processing with a Run Shell Script action (use the following options: Shell: /bin/bash
, Pass input: as arguments
):
Breakdown:
① This workflow asks the user to locate the top-level directory that contains the folders to be archived. Using the example name from above, the user would navigate to the folder 01_FILE_NUMBER
; double-click on it; then click "Open" to beginning processing of its contents.
② A shell script processes the folder's contents as follows:
- The entire top-level folder and its contents are copied to the desktop, which is where you stated you want the final archive to reside.
- Processing is done using this desktop copy, leaving the original untouched.
- All folders three-tiers deep are moved up a level. To illustrate this, consider the example path
01_FILE_NUMBER/728x90/folderA/folderB/728x90/728x90.png
: the tier 1 folder is728x90
; the tier 2 folder isfolderA
; the tier 3 folder isfolderB
. All folders calledfolderB
reside at this third-tier level. They are then moved up one level, and will now reside in the same folder asfolderA
, at tier 2. - All folders two-tiers deep that are not named
folderB
are deleted. This will delete the now-emptyfolderA
folders. - The final folder structure now looks like this:
01_FILE_NUMBER/728x90/folderB/728x90/728x90.png
.
③ A .zip
archive is created on the desktop using the same name as the original top-level folder, i.e. 01_FILE_NUMBER.zip
.
Here is the bash source code to copy-n-paste:
cp -R "$1" ~/Desktop
cd ~/Desktop/"$(basename "$1")"
find . -type d -depth 3 -execdir mv {} ../ \;
find . -type d -depth 2 ! -iname "folderB" -exec rm -R {} +
pwd
Optionally...
The desktop will have both the copied top-level folder and its archived form. You can now safely delete the uncompressed folder from the desktop (the original is still safe in its original location) and keep only the archive. To do this, append a final Run Shell Action at the end of the workflow, using the same Shell and Pass input options as before, and enter this source code:
f="$1"
rm -R "${f%.*}"
open -R "$f" # Reveals the archive in Finder
Warning: Test this workflow with a sample set of files and folders until you're satisfied it does what you want. The workflow permanently deletes folders, and these are not recoverable.