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

8

1

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?

LindaBB

Posted 2016-12-05T21:19:50.437

Reputation: 81

It doesn't appear that copy/xcopy/robocopy support this basic feature... Maybe powershell.. or use %~n1 for destination (%1 being argument to script) see: https://en.wikibooks.org/wiki/Windows_Batch_Scripting#Percent_tilde robocopy /z /e /mt %1 D:\Backups%~n1\

– kevinf – 2018-04-06T00:38:48.127

Answers

6

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

DavidPostill

Posted 2016-12-05T21:19:50.437

Reputation: 118 938

If there are a lot of (small) files it might be a good idea to use the /Q option ("Do not display file names while copying."). At the end of the copy process it will output something like "122546 File(s) copied". – Peter Mortensen – 2018-09-24T11:48:59.457

1

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.

Mark E Rohrer

Posted 2016-12-05T21:19:50.437

Reputation: 11