Robocopy /xd with wildcards and sub-paths

Robocopy definitely won't do what you're looking for on its own.

Initially I thought about telling you to write a script that dynamically generates the exclusion list, but you're likely to run into command-line length limitations.

With that in mind, I think you'd be best off writing a script to create a Robocopy "job" file. Here's a script to create a simple job dynamically based on the contents of C:\Users:

@echo off
set DEST=\\SERVER\SHARE
set SRC=C:\Users
set RCJ=%RANDOM%.RCJ

echo /DD:%DEST%>%RCJ%
echo /SD:%SRC%>>%RCJ%
echo /IF>>%RCJ%
echo *>>%RCJ%
echo /S     :: copy Subdirectories, but not empty ones.>>%RCJ%
echo /E     :: copy subdirectories, including Empty ones.>>%RCJ%
echo /COPY:DAT  :: what to COPY for files (default is /COPY:DAT).>>%RCJ%
echo /PURGE     :: delete dest files/dirs that no longer exist in source.>>%RCJ%
echo /MIR       :: MIRror a directory tree (equivalent to /E plus /PURGE).>>%RCJ%
echo /R:1000000 :: number of Retries on failed copies: default 1 million.>>%RCJ%
echo /W:30      :: Wait time between retries: default is 30 seconds.>>%RCJ%
echo /XD        :: eXclude Directories matching these names>>%RCJ%

for /d %%i in (%SRC%\*) do call :_de "%%i">>%RCJ%

robocopy /job:%RCJ%
del %RCJ%
goto :EOF

:_de
echo %~1\AppData\Local\Google
echo %~1\AppData\Local\Microsoft\Windows
echo %~1\AppData\Local\NVIDIA
echo %~1\AppData\Local\Temp
echo %~1\AppData\LocalLow\Google

I got very cavalier in this script, going ahead and running robocopy and deleting the job file. I'd comment those lines out while you're testing, and probably add some checking of the errorlevel following completion of robocopy.


I'm sorry, but according to the documentation at https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy:

/xf <FileName>[ ...] Excludes files that match the specified names or paths. Note that FileName can include wildcard characters (* and ?).
/xd <Directory>[ ...] Excludes directories that match the specified names and paths.

So this specifically means that wildcard can be used in the /xf flag but not in the /xd flag.