Robocopy is ignoring /xd directories

6,590

robocopy is advertised as the replacement for xcopy but if it's unable to do something simple as what I want then it's truly a terrible replacement.

Any directory name after /xd that matches is excluded. This makes sense and it functions as advertised. Looks like any time you give it anything more than just a directory name then it wets the bed. My problem is surely not so unique looking at some of the other people on the internet and their troubles with robocopy.

I have the following directories:

C:\repo\SomeProject\Data             <- DONT exclude this
C:\repo\SomeOtherProject\Data        <- DONT exclude this
C:\repo\AnotherProject\bar           <- DONT exclude this
C:\repo\Data\foo                     <- Exclude this
C:\repo\Data\bar                     <- Exclude this
C:\repo\Data\baz                     <- DONT exclude this
  • If I call robocopy with /xd Data then C:\repo\SomeProject\Data and C:\repo\SomeOtherProject\Data will also be excluded
  • If I call robocopy with /xd foo bar then C:\repo\AnotherProject\bar will also be excluded.

I want to be able to call robocopy with /xd Data\foo Data\bar so that only foo and bar in Data will be excluded.

Absolute file paths will not work because the code must be portable and I don't really want to inject absolute paths for each entry in my >100 excludes list. It just doesn't seem right.

What appears to happen is if I use /xd Data\bar then robocopy interprets it as Data\\bar which it cannot find. I do not know why a single \ is changed to \\ and any permutation of \ or / does not work either.

The solution

So ultimately the answer to my question is to use xcopy. I give it a list containing entries such as *.cpp, 8.vcxproj, \Data\foo\, \Data\bar\ and a 100 other entries and it seems to work just fine excluding all the stuff I don't want while keeping the stuff I need.

Share:
6,590
getack
Author by

getack

Updated on September 18, 2022

Comments

  • getack
    getack over 1 year

    I am trying to use Robocopy to create client software builds by copying all the necessary files out of our main repository. We have multiple clients each needing their own custom software builds.

    I have a list of requirements for each client's build. My thinking is to copy over the core stuff first (binaries and such, used by all clients) and then copy over the client specific stuff depending on for whom I'm making a build by making use of these lists. We have something like this already working with xcopy.

    There is obviously a bunch of stuff I want excluded from the client build such as source files, log files and obviously all the client specific stuff. I thought by making clever use of the /xf and /xd switches I should be able to get it to work.

    After a getting all the needed info, the following command is built in Lua:

    robocopy "Z:\path\to\source" "../dest"  /e /xf *.cpp *.h *.hpp [[. . .]] *.cxx   /xd Data/Testing Data/Some/Client/Data Data/Other/Client/Data [[ . . .]] Data/More/Directories 
    

    When running it I get the following output:

    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows                              
    -------------------------------------------------------------------------------
    
      Started : Tuesday, August 1, 2017 11:15:59 AM
       Source : Z:\path\to\source
         Dest : ../dest
    
        Files : *.*
    
    Exc Files : *.cpp
            *.h
            *.hpp
            [[. . .]]
            *.cxx
    
     Exc Dirs : Data/Testing
            Data/Some/Client/Data
            Data/Other/Client/Data
            [[ . . .]]
            Data/More/Directories
    
      Options : *.* /S /E /DCOPY:DA /COPY:DAT /R:1000000 /W:30 
    
    ------------------------------------------------------------------------------
    

    The output tells me my command is formatted properly and that Robocopy understands what I'm asking it to do.

    The problem is that it's straight up ignoring the Exc dirs list and is just copying everything. I do not want to exclude the whole Data directory, but only the bits inside it that are relevant.

    If I use back slashes (\) then the Exc dirs output is printed like Data\\testing. Could this be my issue?

    • Seth
      Seth almost 7 years
      Did you try to use absolute paths? Did you try to build a small example to see what kind of slashes you need?
    • getack
      getack almost 7 years
      I am trying to avoid absolute paths because the scripts must be able to run on any machine, with the repository installed anywhere. The script is part of the main repository, so we use relative paths then. I am building a small example now to test the theory of the slashes.
    • Vomit IT - Chunky Mess Style
      Vomit IT - Chunky Mess Style almost 7 years
      If you don't want to use full explicit paths of the excluded then just use the sub folder name only of some top level subfolder name, etc. to tell the job to exclude i.e. /XD "Testing Data" "More" and that should work but for all subfolders matching that name so you may need to rethink the folder naming for the dirs you want to exclude and append a character to make them unique if the name is not unique otherwise per the job so "/z_Testing Data" for example. Otherwise, consider putting dirs to not backup in a standard top level parent folder e.g. \NoBackup\Testing Data. Simple to test.
    • getack
      getack almost 7 years
      Our repository is almost 20 years old. Changing the directory layout to something more sensible is unfortunately not possible due to technical inertia.
    • Seth
      Seth almost 7 years
      I don't really get why you would not be able to use absolute paths. Your original question states you're generating the robocopy line using LUA and also already include an absolute path. But well, it seems you've found an answer that works for you. Interesting though that your XCOPY approach works as it should have the same behavior. Maybe exclude the full example of what you did in your answer, as it might be helpful.