Robocopy /XD Won't Work With Relative Paths

12

1

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

Posted 2013-12-19T21:40:39.810

Reputation: 395

3It 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 – 2016-10-04T11:18:52.683

gbr answer should be the correct answer. (given you don't need ".." in the relative path) – Sake – 2018-02-14T08:45:54.517

Answers

13

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

Posted 2013-12-19T21:40:39.810

Reputation: 103 763

6Unofortunately this doesnt work for subdirectories in level 2 and higher i.e. /XD "SubDir1/SubDir11". – truthseeker – 2014-11-29T19:52:03.047

@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 – 2018-01-22T17:23:30.907

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

Big John

Posted 2013-12-19T21:40:39.810

Reputation: 1