How to use Robocopy to copy a folder structure into all (sub)folders located under one directory?

19,018

I am not quite sure if I understand every aspect of your question, but here we go.

Save this code to a CMD file and customize the source, destination and robocopy.exe path where destination means the root folder which contains your 488 subfolders.

@echo off
SET "source=C:\Users\zsmall\Desktop\Attachment Structure"
SET "dest=C:\Users\zsmall\Desktop\CustomTest"    
DIR /A:D /B %dest% > folders.txt    
for /f "delims=" %%G in (folders.txt) do (
C:\Windows\System32\robocopy.exe "%source%" "%dest%\%%G" /copy:DAT /E /W:2 /R:2 /MT:8
)
pause

What it does

  • lists all folders (no subfolders) from your destination folder and writes the names to a text file
  • loops through every folder in that list file
  • executes a robocopy command with the current folder as destination
    (source is always the same folder)
  • robocopy copies all folders, subfolders and files including data, attributes and timestamps

For information about the used parameters, look here:

Don't try to do it without saving the list first in a textfile. Or you may end up like me, who was creating thousands of subfolders with the same name.

Share:
19,018

Related videos on Youtube

small3687
Author by

small3687

I just want to learn and appreciate any help in that endeavor.

Updated on September 18, 2022

Comments

  • small3687
    small3687 over 1 year

    Is there anyway for me to quickly take an empty folder structure I have created on my desktop and copy it to 488 other folders all located on a network drive but in the same folder?

    My primary difficulty with this is that I want the folder structure I have created to be placed in the sub folders of a networked folder. There are 488 folders where my structure must be copied to. They all contain additional folders and files themselves, which I do not want to have any effect on. Is there a way to set robocopy destination folder to a 1 level lower or deeper than the destination folder? Similar to /LEV:n but reversed essentially?

    I have seen some postings mentioning recursion in what I believe to be are similar situations but I do not know how to execute a recursive function if that is possible.

    • MaQleod
      MaQleod over 11 years
      You should be able to use the /mir option with robocopy to mirror the directory tree.
    • TheCompWiz
      TheCompWiz over 11 years
      @MaQleod the "mir" flag does not do bi-directional replication. I know of only 1 tool that does this fairly well. Try looking at Unison. cis.upenn.edu/~bcpierce/unison
    • MaQleod
      MaQleod over 11 years
      @TheCompWiz: It didn't sound like he wanted bi-directional replication to me, it sounded like he wanted to take an empty directory tree on his PC and re-create it on a network drive.
    • small3687
      small3687 over 11 years
      @MaQleod Please excuse me if I have misunderstood but I thought /mir would then also purge any data in the destination location that wasn't also in the source. I do not wish to delete anything from the destination. My source is a folder containing two more folders which contain five folders each. There are no files in these but the destination folder contains, pdfs and excel files that need to remain. Also my primary frustration is that the destination is 488 separate folders residing in one "customers" folder. Am I misunderstanding /mir?
    • nixda
      nixda over 11 years
      @small3687 I'm glad to see that its working :) But one small suggest I have to do: Please don't use questions or answer for a simple "Thank you". The green check mark and comments are meant for this job. Also, its always good to delete obsolte comments so others will find the important information faster.
  • small3687
    small3687 over 11 years
    Thank you so much for your help! This is the first time I have tried to do anything with a command line so I am a little confused and I haven't been able to get it to work just yet so a little help would be greatly appreciated. I have gotten it generate the the text file with the appropriate names of the folders but I haven't been able to get it to properly execute past that. I receive errors stating that no destination directory is specified. My code is as follows:
  • small3687
    small3687 over 11 years
    @echo off SET "source=C:\Users\zsmall\Desktop\Attachment Structure:\" SET "dest=C:\Users\zsmall\Desktop\CustomTest:\" DIR /A:D /B %C:\Users\zsmall\Desktop\CustomTest% > folders.txt for /f "delims=" %%G in (folders.txt) do ( C:\Windows\System32\robocopy.exe "%C:\Users\zsmall\Desktop\Attachment Structure:\%" "%C:\Users\zsmall\Desktop\CustomTest\%\%%G" /copy:DAT /E /W:2 /R:2 /MT:8 ) pause
  • small3687
    small3687 over 11 years
    Success! With some interesting notes. For some reason the MT:8 Parameter for multithreaded copying was producing an Error stating Invalid Parameter #7. To remedy this I simply tried deleting the line and running the command, which worked successfully. As you can see my source file was labeled Attachment Structure, which I wanted included when being copied to the destination files but it only included the folders inside that one. To solve that I simply made a second copy folder named Attachment Structure inside the original and the program behaved perfectly. Thanks so much :)
  • nixda
    nixda over 11 years
    The MT:x parameter is not available for every robocopy version. Basically there are 3 main versions (XP016, XP026, XP027). This parameter was introduced with XP027.