[robocopy] How to exclude subdirectories in the destination while using /mir /xd switch in robocopy

There is a script running which mirrors a bunch of folders from one volume to another. The problem is that now there are going to be subdirectories within those folders at the destination which are not part of the original mirroring script. They are standalone subdirectories and I don't want them purged once the mirroring kicks in. Is there a way for me to use the /xd switch in robocopy wherein I'd be able to exclude the destination subdirectories.

Example:

robocopy "\\hq04t2fis202\archive\dr" "\\hq04t3fis202\archive\dr" /mir /xd "\\hq04t3fis202\archive\dr\*\hq04s2dba301"

In the above example, there are several directories under \\hq04t2fis202\archive\dr that are being mirrored. And at the destination, once these directories are mirrored from the source, there is another script which dumps separate subdirectories within each of those directories. So what I want to do is somehow use the /xd switch to avoid purging those subdirectories at the destination. Also, in the above example, in the /xd switch, I'm using the "*" wildcard to mean that I want to include all the directories that fall under the "\\hq04t3fis202\archive\dr" root folder at the destination.

Need help and suggestions as to whether this is possible, and if it is then how can I do it. So far I've tried and tested many other switches like /xo, /xx, but none of them solve my purpose. Also, /xx would work except that now it won't delete any folders at the destination at all and it wouldn't be mirroring.

Hopefully I'm not overly confusing everyone here. Let me know if you have any questions.

This question is related to robocopy

The answer is


When i tried the solution with /XD i found, that the path to exclude should be the source path - not the destination.

e.g. this Works

robocopy c:\test\a c:\test\b /MIR /XD c:\test\a\leavethisdiralone\

The argument order seems to matter... to exclude subdirectories, I used

robocopy \\source\folder C:\destinationfolder /XD * /MIR

...and that works for me (Windows 10 copy from Windows Server 2016)


Try my way :

robocopy.exe "Desktop\Test folder 1" "Desktop\Test folder 2" /XD "C:\Users\Steve\Desktop\Test folder 2\XXX dont touch" /MIR

Had to put /XD before /MIR while including the full Destination Source directly after /XD.


Rather than creating empty directories in source to exclude, you can supply the full destination path to the /XD switch to have the destination directories untouched

robocopy "%SOURCE_PATH%" "%DEST_PATH%" /MIR /XD "%DEST_PATH%"\hq04s2dba301

The issue is that even though we add a folder to skip list it will be deleted if it does not exist.

The solution is to add both the destination and the source folder with full path.

I will try to explain the different scenarios and what happens below, based on my experience.

Starting folder structure:

d:\Temp\source\1.txt
d:\Temp\source\2\2.txt

Command:

robocopy D:\Temp\source D:\Temp\dest /MIR

This will copy over all the files and folders that are missing and deletes all the files and folders that cannot be found in the source

Let's add a new folder and then add it to the command to skip it.

New structure:

d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
d:\Temp\source\3\3.txt

Command:

robocopy D:\Temp\source D:\Temp\dest /MIR /XD "D:\Temp\source\3"        

If I add /XD with the source folder and run the command it all seems good the command it wont copy it over.

Now add a folder to the destination to get this setup:

d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
d:\Temp\source\3\3.txt

d:\Temp\dest\1.txt
d:\Temp\dest\2\2.txt
d:\Temp\dest\3\4.txt

If I run the command it is still fine, 4.txt stays there 3.txt is not copied over. All is fine.

But, if I delete the source folder "d:\Temp\source\3" then the destination folder and the file are deleted even though it is on the skip list

                       1    D:\Temp\source\
    *EXTRA Dir        -1    D:\Temp\dest\3\
      *EXTRA File                  4        4.txt
                       1    D:\Temp\source\2\

If I change the command to skip the destination folder instead then the folder is not deleted, when the folder is missing from the source.

robocopy D:\Temp\source D:\Temp\dest /MIR /XD "D:\Temp\dest\3"

On the other hand if the folder exists and there are files it will copy them over and delete them:

                       1    D:\Temp\source\3\
          *EXTRA File                  4        4.txt
100%        New File                   4        3.txt

To make sure the folder is always skipped and no files are copied over even if the source or destination folder is missing we have to add both to the skip list:

robocopy D:\Temp\source D:\Temp\dest /MIR /XD "d:\Temp\source\3" "D:\Temp\dest\3"

After this no matters if the source folder is missing or the destination folder is missing, robocopy will leave it as it is.


The way you can exclude a destination directory while using the /mir is by making sure the destination directory also exists on the source. I went into my source drive and created blank directories with the same name as on the destination, and then added that directory name to the /xd. It successfully mirrored everything while excluding the directory on the source, thereby leaving the directory on the destination intact.