Search code examples
copyxcopyrobocopy

How to copy a file from source to a destination and recreate the folder structure


I have some files which needs to be copied through deployment process often to a destination. This is my folder folder structure:

SOURCE:

c:\
   folder1
          sub1
              subsub1
                     file1
                     file2

I need something where i can tell my "script" something like this

mycopy c:\folder1\sub1\subsub1\file1 h:\

That means that i dont want to

  • check if folder structure exist
  • provide on bot sides the complete structure for each file on destination side

I want to

  • provide the full path and filename on source side
  • create folder structure if not exist
  • overwrite file if exist

How can i achieve this?


Solution

  • Ok it works like this:

    You can use Robocopy for this task. Thanks you Rick716 for this direction. I am not marking it as answer cause it is only a direction not the solution.

    Lets Assume that we have the following source folder structure:

    N:\source\a1\b1\c1\d1\e1\f1
    

    and we want to recreate the hole structure under n:\source within h:\destination. Then we have to use Robocopy in this way:

    robocopy N:\source h:\destination /e
    

    The option /e will create the folder structure even create empty folders. Additionally you can append the files which should be copied. For example

    file.txt
    *.jpg
    *.bkp
    

    etc. But these files will be even copied when they exist somewhere within the folder strcuture! For example you have the file n:\source\a1\file.txt and the file n:\source\a1\b1\c1\file.txt then both will be copied by using

    robocopy N:\source h:\destination file.txt /e