Search code examples
bashapplescriptdirectoryautomatorrobocopy

Copy folder structure and merge sub directories


enter image description hereI'm not sure where best to post this, hopefully someone can advise if this isn't right, I've been a long time browser but this is my first post. Anyway..

I need to automate copying a load of folders to a new location and merging sub directories.

I will use ABC as an example text string which represents the company name and is a constant value.

The current structure is like:

  • 2012 Jobs

    • ABC12 001-099
      • ABC12-001
        • jpgs
        • raw
        • web
        • tiffs
      • ABC12-002
      • ABC12-003
      • ...
    • ABC12 100-199
    • ABC12 200-299
  • 2013 Jobs

    • ...
  • 2014 Jobs
    • ...

What I need to do is copy this structure to a new location, but merge the jpgs and web folders in to one named jpgs for example and the raw and tiffs in to another folder named tiffs.

I created an AppleScript to create the folder structure but these are obviously all just blank folders, would be much easier to just copy what is there already and start again, open to any suggestions, you're the experts! Thanks

Image shows structure before and how I want it after: Before and After


Solution

  • The script bellow does partially what you need (I think !).

    It looks for every "raw" in the top folder you selected (i.e. the folder containing all the "2012 jobs", 2013 Jobs",...)

    For each "raw" folder found, it looks if, at same level a "tiffs" folder already exists (if not it creates it).

    Then it moves all the files from "raw" to the "tiffs". Finally, it deletes the empty "raw".

    set TopFolder to choose folder with prompt "Select parent folder to process"
    
    tell application "Finder"
    set RawsFolder to every folder of entire contents of TopFolder whose (name is "raw")
    repeat with aFolder in RawsFolder
        set ParentFolder to container of aFolder -- get upper folder
        set TiffFolder to (ParentFolder as string) & "tiffs"
        if not (exists TiffFolder) then make new folder in ParentFolder with properties {name:"tiffs"}
        set FilesRaw to every file of aFolder --get all files from raw folder
        move FilesRaw to folder TiffFolder -- move them to tiffs folder
        delete aFolder -- delete the raw folder
    end repeat
    end tell
    

    To complete, you must do same thing again, but with "web" folders to be copied into "jpgs". You have 2 ways to do that :

    1) move the "tell Finder" block into sub routine with 2 strings parameters (raw, tiffs), adjusting the script to replace each occurrence of raw/tiffs by the parameter. then you call this routine with ("raw", "tiffs") and again with ("web","jpgs")

    2) you just copy/paste the tell finder block and change "raw"/"tiffs" manually to "web" / "jpgs"

    Up to you...

    Warning : don't forget to run this on a full copie of your parent folder !