23

I am trying to run a robocopy /MOVE command but exclude a couple of sub-directories specified with the /XD flag.

robocopy ".\CurrentDir" ".\NewDir" /XD ".\CurrentDir\SubDir1" ".\CurrentDir\SubDir2" /E /MOVE

Unfortunately it ignores the /XD option and moves everything. If I use an absolute path on everything, it works fine, but I need this to be flexible. Is there any way to make it work with relative paths?

user8783
  • 515
  • 2
  • 4
  • 9
  • 7
    It should be noted that you can turn relative paths into absolute ones with less effort than one might think, by using the `%CD%` environment variable. Example: `robocopy ".\CurrentDir" ".\NewDir" /XD "%CD%\CurrentDir\SubDir1" "%CD%\CurrentDir\SubDir2" /E /MOVE` – gbr Oct 04 '16 at 11:18
  • gbr answer should be the correct answer. (given you don't need ".." in the relative path) – Sake Feb 14 '18 at 08:45

2 Answers2

19

The folders specified for /XD are referring to folders (to exclude) that exist within the source folder, so you only specify the folder name(s) you wish to exclude, without specifying a parent relation.

ie:

robocopy ".\CurrentDir" ".\NewDir" /XD "SubDir1" "SubDir2" /E /MOVE
Ƭᴇcʜιᴇ007
  • 111,883
  • 19
  • 201
  • 268
  • 10
    Unofortunately this doesnt work for subdirectories in level 2 and higher i.e. /XD "SubDir1/SubDir11". – truthseeker Nov 29 '14 at 19:52
  • 1
    @truthseeker - you're right. It works for 1st-level folders, but nothing lower. For that, you have to use absolute source path (ie. "/xd %~dp0CurrentDir\Folder1\Subfolder1"), then it will properly ignore "Subfolder1" but sync "Folder1" etc. You can't use relative path, and you can't use destination path. – rocketmonkeys Jan 22 '18 at 17:23
0

The following may help - it does address the relative path issue directly - but does allow for many exclude directories and sub directories.

If you are just exclude top level folders - just use the folder name - example APC. However if you are doing a sub folder - you need the entire path. The path must fully match the source path + path down to the subfolder. I have tested it many different ways and this is the only way I was able to get it to work. This batch/cmd file format also allows for spaces in the path or file names.

setlocal
set Source_path="\\usatlvmpdroadm1\C$\Installs"
set Destination_path="C:\Robo"
set Log_Path_and_File="C:\RoboCopy_Log_Files\01-Installs C drive to Robo C drive.txt"
set ED1="APC"
set ED2="\\usatlvmpdroadm1\C$\Installs\Dell\Dell MD3420 Firmware"

robocopy.exe %Source_path% %Destination_path% /COPYALL /ZB /MIR /MT:128 /DCOPY:T /XA:SH /XD %ED1% /XD %ED2% /XJ /XJD /R:2 /W:1 /NDL /NP /TEE /LOG:%Log_Path_and_File%

endlocal
JSK NS
  • 150
  • 7