Use Automator to copy multiple folders with different amounts of folders while removing parent folder

applescriptautomator

  1. copy whats within the red box(images folder too with content) into the blue box and delete the folders.

  2. the remaining result will then be SIZExSIZE – > sizexsize.file etc.. and images folder with images.

enter image description here

Best Answer

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 called folderA, and nothing else;
  • Each folder called folderA contains a single folder called folderB, and nothing else;
  • Each folder called folderB contains a single folder named identically to the 123x456 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 form 01_FILE_NUMBER/728x90/folderA/folderB/728x90/728x90.png. Importantly, folderA has no siblings, and folderB 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):

Automator Workflow

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:

  1. 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.
  2. Processing is done using this desktop copy, leaving the original untouched.
  3. 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 is 728x90; the tier 2 folder is folderA; the tier 3 folder is folderB. All folders called folderB reside at this third-tier level. They are then moved up one level, and will now reside in the same folder as folderA, at tier 2.
  4. All folders two-tiers deep that are not named folderB are deleted. This will delete the now-empty folderA folders.
  5. 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.