florischu

Q: Automatically copy files into subfolders

Hi everyone,

 

I hope I'm posting this in the correct forum - sorry if that's not the case.

 

I'm trying to find out how to write a script or automator instruction to copy selected files into a folder and also add copies into all it's subfolders automatically.

 

Basically, I have folder A - which contains two files - and folder B - which contains many files and folders, and many subfolders (many of which also have subfolders). I need an automated process where I can have the two files in folder A copied into each and every folder and subfolder in folder B.  It's way too time consuming to do this manually.

 

Does anyone know of a method to automate this?

 

Appreciate any help I can get

 

Thanks,

 

Florian

MacBook Pro (Retina, 13-inch, Late 2013), OS X Yosemite (10.10.1)

Posted on Jan 28, 2015 5:33 AM

Close

Q: Automatically copy files into subfolders

  • All replies
  • Helpful answers

  • by Camelot,Solvedanswer

    Camelot Camelot Jan 28, 2015 10:38 AM in response to florischu
    Level 8 (47,243 points)
    Mac OS X
    Jan 28, 2015 10:38 AM in response to florischu

    This is pretty easy to do with a recursive script. You do need to be careful, though, that the script doesn't run away and do more than you want/expect.

     

    It isn't clear if this is a one-off script, or if this is something you want to do repeatedly, so the following script might not be ideal, but it asks you for the two folders and copies every file in FolderA into every folder of FolderB:

     

    --prompt the user for the source files/folder

    set folderA to (choose folder with prompt "Select the folder with the source files")

    set folderB to (choose folder with prompt "Select the destination folder")

     

    -- find the files in the source folder

    tell application "Finder" to set sourceFiles to every file of folderA as alias list

     

    -- copy the files to the destination

    my copyFiles(sourceFiles, folderB)

     

     

    on copyFiles(theFiles, theFolder)

      log theFolder

      -- copy the files

      tell application "Finder"

      try

      duplicate theFiles to folder theFolder

      end try

     

      -- iterate through any subfolders:

      -- get a list of subfolders

      set subFolders to (get every folder of theFolder as alias list)

      -- recursively copy the files into those folders

      repeat with eachFolder in subFolders

      my copyFiles(theFiles, eachFolder)

      end repeat

      end tell

    end copyFiles

  • by florischu,

    florischu florischu Feb 7, 2015 2:32 AM in response to Camelot
    Level 1 (1 points)
    Feb 7, 2015 2:32 AM in response to Camelot

    Thanks Camelot. Appreciate your help.

  • by touchscreenman,

    touchscreenman touchscreenman Jul 19, 2016 12:00 PM in response to Camelot
    Level 1 (4 points)
    Mac OS X
    Jul 19, 2016 12:00 PM in response to Camelot

    I have a similar scenario to florischu. I have a folder than contains main sub folders and then files, this is stored on a server. I would like to copy the files with a script like this to a local folder on the computer, not needing to maintain the folder hierarchy.