Using xCopy to create entire folder structure, including root folder name and all files

I looked at quite a few solutions to xCopy questions, and tried many difference methods. (Various wildcards, paths ending in \, various xcopy switches in various combinations.)

xCopy c:\Public  d:\MyNewDir\

When done, I need the destination to include a folder called "Public" with containing all files, folders, subfolders, everything.

The result should will look like:

d:\MyNewDir\Public\(and everything inside it)

Not like this:

d:\MyNewDir\(everything inside Public)

That sounds so simple. Instead, I never see a "Public" folder created. It only creates everything WITHIN "Public".... but never "Public" itself. (I have many folders to copy, so I don't want to create folders individual, manually.)

Is there a solution to this simple issue using only xCopy and Windows 7?


I need the destination to include a folder called "Public"

containing all files, folders, subfolders, everything.

Use the following command:

xcopy c:\Public\* d:\MyNewDir\Public /s /i
  • /s - Copy folders and subfolders

  • /i - If in doubt always assume the destination is a folder e.g. when the destination does not exist.


Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • xcopy - Copy files and/or directory trees to another folder.

1st, enumerate folder structure into a file:

dir /ad /b /s C:\ > D:\windir.txt

2nd, open D:\windir.txt in Notepad and replace all C:\ with null; save file

3rd, use for command to recurse through windir.txt to copy directory structure and files in each directory:

for /f "delims=;" %a in (D:\windir.txt) do xcopy "C:\%a" "D:\MyNewDir\%a" /c /i /g /h /k /o /x /j /b /y

You can add /q if you don't want to see the directories and files as they're being copied; I like the positive feedback.