8

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?

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
LindaBB
  • 81
  • 1
  • 1
  • 3
  • 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\ – Kevin Apr 06 '18 at 00:38

2 Answers2

9

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
  • 153,128
  • 77
  • 353
  • 394
  • 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 Sep 24 '18 at 11:48
  • I'm going mad. Again it's not copying the root directory for me – Hossein Ebrahimi Aug 08 '23 at 08:03
  • I've succeeded with [Copy-Item](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.3#example-3-copy-directory-and-contents-to-a-new-directory) – Hossein Ebrahimi Aug 08 '23 at 08:14
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.