Is There Windows Copy Command That Mimic Unix `Cp -R` Behavior?

I don't know how to name this behavior vividly, just call it loyalcopy.

Two figures will illustrate it clearly.

I'd like to issue two commands to copy two filesystem nodes (regardless it is a file for a directory):

loyalcopy c:\libsrc\include     d:\temp\dest
loyalcopy c:\libsrc\mm_psfunc.h d:\temp\dest

and see result like this:

What actual Windows commands can achieve this effect? I tried xcopy and robocopy, and they do not work as desired. For example, xcopy c:\libsrc\include d:\temp\dest /s would copy contents inside include, not creating a mirrored include directory in destination folder.

On Unix, I know I can replace loyalcopy with cp -r to achieve desired result. But is there Windows equivalent of it? Better to have Windows stock commands, no third-party software involved.

2 Answers

Just run cp -r c:\libsrc\include d:\temp\dest in PowerShell. It'll work as expected. It's actually the alias to

Copy-Item -Recurse c:\libsrc\include d:\temp\dest

With xcopy or robocopy you'll need to specify the folder name in the destination in order for it to be created

robocopy /E c:\libsrc\include d:\temp\dest\include
xcopy /E /I /H c:\libsrc\include d:\temp\dest\include

/I: If Source is a directory or contains wildcards and Destination does not exist, xcopy assumes Destination specifies a directory name and creates a new directory. Then, xcopy copies all specified files into the new directory. By default, xcopy prompts you to specify whether Destination is a file or a directory.

To get help on any classic Windows tools just use /?, for example xcopy /?, robocopy /?. Note that xcopy has been deprecated and may be removed in the future

1

Try this:

xcopy /h /e /i "c:\libsrc\include\" "d:\temp\dest\include\"
xcopy /h /e /i "c:\libsrc\mm_psfunc.h" "d:\temp\dest\mm_psfunc.h" 
3

Your Answer

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

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest