27

I can't believe that I am having trouble in copying a directory into another directory via the windows command line.

What I want to do is simple -:

Lets say I have a directory -:

C:\test

and I want to copy test to D: So in D there should be a folder like the following-:

D:\test

when I use

robocopy C:\test D:\test \E

D drive ends up with the contents of C:\test in the root rather than being contained in a directory called test.

How do you do this simple thing ?

ng.newbie
  • 509

4 Answers4

39

If you want to create an exact duplicate, use the following version (which is equivalent to adding /E and /PURGE:

robocopy c:\source d:\destination /MIR

If all you want to do is copy the directories and subdirectories including empty ones, use

robocopy c:\source d:\destination /E

It is the backslash on the E option that was getting you.

To learn more about Robocopy here is a handy search:

http://www.google.com?q=robocopy+syntax

Frankie
  • 274
SDsolar
  • 1,696
5

It seems to me that usage of robocopy in the previous answers do not actually answer the question, that is, to copy the source folder, with its contents, to the destination folder.

The following is what worked for me. It preserves the creation dates and logs activities both to a file and to the screen. It also retries to read a file up to 5 times if inaccessible. Feel free to modify the flags to suit your needs.

robocopy 'G:\Some Directory\A ' 'E:\Some Directory\A ' /e /dcopy:T /mt /tee /log:A.log /r:5
  • /e copies subdirectories including empty directories
  • /dcopy:T includes timestamps
  • /mt multitreaded (8 threads by default)
  • /tee write status to both console and log
  • /log:A.log writes status to log file A.log
  • /r:5 retries 5 times on copy failure

Two observations that seemed non-obvious to me:

  • The path strings need to end with a space in order for the source root directory to be created at the destination, and not only its contents without the containing directory.
  • The root directory at the destination needs to be in the destination path, even though it doesn't exist before issuing the command.
    • The original creation date of the root directory will still be preserved.

I find it surprising that an operation as commonly desired as the one asked about is not better documented.

ivvi
  • 201
  • 2
  • 9
1

Use Robocopy (Robust File Copy)

robocopy c:\test d:\test /s /e *.*

/s switch is for copying all sub directories and /e switch is for copying all the empty sub directories & *.* means (all files).(any extensions)

syntax:

robocopy source-folder destination-folder files switches

Please visit this link for more information about robocopy.

0

Although this answer doesn't use robocopy, I think it still does the purpose of copying directories.

You could try using: xcopy

Usage: xcopy source [destination] ...

You could check for more details by executing xcopy /?

Source: HOW TO: Copy a Folder to Another Folder and Retain its Permissions.

MarianD
  • 2,726