2

How can a folder be copied using the cmd.exe shell?

Using Explorer, this is easy to do. Right-click the folder to copy, drag it to the destination folder, and then select "Copy here."

Suppose we have this folder structure:

src
  --a.txt
  --b.txt
dest
  --c.txt
  --d.txt

How can we get this result:

src
  --a.txt
  --b.txt
dest
  --c.txt
  --d.txt
    src
      --a.txt
      --b.txt

The command copy src dest does not do this -- it copies a.txt and b.txt into dest instead of the src folder itself.

Nor does it work as xcopy /e src dest.

Is there a command that does this using built in tools?

3

Try this:

xcopy /e src dest\src\

You need to tell xcopy to create a directory under the dest.

If you really hate typing src twice and you have rsync installed, the following will do what you want:

rsync -a dir1 dir2/
3

Use robocopy. Specifically, with the /E flag.

robocopy /e src dest\src
  • Did you actually try? The end result is no different than executing xcopy /e src dest. – and31415 Feb 18 '14 at 23:38
  • 1
    Robocopy is a replacement for xcopy. It has a lot more capabilities (like multithreading, on newer Windows versions) and especially if working with long file names is much preferable. Yes, xcopy might work, but robocopy is better moving forward. Check en.wikipedia.org/wiki/Robocopy#Features for details on why it's better than xcopy. – dr.nixon Feb 19 '14 at 14:48
  • The issue is not xcopy vs robocopy, the issue is which one copies the folder? I edited your answer to provide a working example -- it is required to provide the destination folder. – Kevin Panko Feb 19 '14 at 15:05
  • I won't argue about robocopy being a more powerful tool. The question was edited, but running robocopy /e src dest wouldn't create a src directory inside dest, unless you explicitly specify it. – and31415 Feb 19 '14 at 15:06
1

The question is tagged with cmd.exe but PowerShell is included standard since Windows 7

In PowerShell, this can be done easily as:

Copy-Item -Recurse src dest

or, shorter but the same:

cp -r src dest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.