4

I've located this KB article which explains the topic title. But what my problem is that when I use "xcopy" command; it only copies the contents inside a directory to the new location but not the whole directory like in linux distros.

For example: If I type "xcopy c:\folder1 d:\newfolder"; then it copies the contents inside c:\folder1 to d:\newfolder but not the folder1. I'm quiet confused as in how am I to achieve this. Please advise me on this.

Thank you in advance.

4
  • 2
    Have you tried using Robocopy instead ?
    – Lawrence
    Jan 2, 2014 at 11:13
  • I guess robocopy is the same as it does not copy the specified folder itself instead copy only the contents inside.. that is what I'm confused..
    – AzkerM
    Jan 2, 2014 at 11:46
  • 1
    Maybe try and put folder1 into a subfolder, and then start from a parent folder ? e.g. robocopy C:\subfolder\ d:\folder
    – Lawrence
    Jan 2, 2014 at 12:12
  • that's a good idea. but I wonder what will happen to the permissions. will give it a try and see. thanks a lot
    – AzkerM
    Jan 2, 2014 at 12:57

3 Answers 3

14

I finally managed to achieve this & I thought may be this could help others to accomplish such needs. Well, as I've mentioned earlier I referred to this KB article and used below commands to achieve without any loss of DATA's

I couldn't copy the directory itself but I manage to copy all the contents inside it without loosing permission. So here's how

Firstly, I created a folder just to copy all the contents lies inside the source directry & I used xcopy to copy all files and folders by retaining its permissions.

xcopy c:\source_folder_name e:\destination_folder_name /O /X /E /H /K
  • /E - Copies folders and subfolders, including empty ones.
  • /H - Copies hidden and system files also.
  • /K - Copies attributes. Typically, Xcopy resets read-only attributes.
  • /O - Copies file ownership and ACL information.
  • /X - Copies file audit settings (implies /O).

This will do without any further questions. And once done I noticed there was a file count difference between the copied and origin. Therefore, I used robocopy to make sure all files are in place by creating a log file while mirroring.

Assuming your command prompt path is C:/..

robocopy c:\source_folder_name e:\destination_folder_name /MIR /LOG:log.txt
  • /MIR - Mirror files and copy/modify/delete between source and destination.
  • /LOG - Creates a log file of the process

Note: log file will be created in location where the drive or the path which is shown on the command prompt. E.g.: if its C:/, then the log will be in there).

Once robocopy did its part, I once checked the properties to make sure both are tallying. Heavenly yes! it was tallying and I checked the log to see which file wasn't copied. As I suspected it was just thumbs.db & few of other files which was omitted.

P.S.: Be aware when you're using robocopy for mirroring as it may remove/modify source files by any chance it if couldn't tally with destination (very rare, but may happen). I've used it because the filed count on the new destination was lower than the origin. Also, I had a back copy of the source file just to avoid any DATA loss. Other than, both these commands will execute without causing any trouble at all.

Hope this helps!

2
  • For top level folder perms, (1) create child folder within source folder. (2) Properties -> Security -> Advanced -> Disable Inheritance -> Convert. (3) Use accepted xcopy on the top level folder. Now the child is at the target location just 1 dir below with good perms. Rename/Rearrange using 'mv' (not copy). Works with remote drives.
    – crokusek
    Jun 7, 2019 at 19:40
  • > /E - Copies folders and subfolders, including empty ones; According to MS Docs for xcopy, subfolders also requires /S flag...in 2020?
    – xtian
    Oct 17, 2020 at 15:28
0

The only way is to specify folder1 again. /I switch will stop XCOPY asking silly questions about whether folder1 is a file or a directory. /E to recurse.

xcopy /I /E c:\folder1 d:\newfolder\folder1

Edit: I didn't read the previous KB article so you might have a permissons issue there, which possibly defeats the point. In which case Lawrence might have a point.

0

Yes the best option is to specify the file name again... For eg. I need to copy the C:\Inetpub folder to another folder or drive.

XCOPY "C:\Inetpub" "C:\DDRIVE\Inetpub" /o /x /e /h /k

Once you press enter it will ask " Inetpub Specify a directory name or a File name on the target?". Press F for file and press D for directory. You can press D here. This will create a new directory and copy all the sub folders and files with the same permissions from the source.

You must log in to answer this question.

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