2

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

Two figures will illustrate it clearly.

before copy

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:

after copy

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 2

5

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 -Path c:\libsrc\include -Destination 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.

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy

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
  • Thank you for the points. cp -r in Powershell works as expected. xcopy /i is still undesired, because we users have to explicitly pass /I when copying c:\libsrc\include but omit /I when copying c:\libsrc\mm_psfunc.h. That is extra burden.
    – Jimm Chen
    Commented Mar 12, 2022 at 2:42
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
  • No luck. 0 File(s) copied. xcopy seems to apply wildcards on filenames, never on folder names.
    – Jimm Chen
    Commented Mar 11, 2022 at 10:25
  • @Jimm Chen please see updated answer Commented Mar 11, 2022 at 11:18
  • 1
    See xcopy /? for all flags. You may want to add /O as well to copy also file-permissions and owners ship information. If you don't ALL destination folders/files will inherit their permissions from the parent folder. "/O" may need admin-rights in order to work properly though.
    – Tonny
    Commented Mar 11, 2022 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .