大网数据广告活动
服务器运维问答

Robocopy /xd with wildcards and sub-paths

· 52CE 我爱测

I don't think this exact question has been asked before (I looked). My backup server runs a script using Robocopy, and I want to exclude files that don't really need to be backed up, specifically: *\AppData\Local\Google *\AppData…

问题原文

原帖链接 · nerdfever.com · CC BY-SA 3.0

I don't think this exact question has been asked before (I looked).

My backup server runs a script using Robocopy, and I want to exclude files that don't really need to be backed up, specifically:

*\AppData\Local\Google 
*\AppData\Local\Microsoft\Windows 
*\AppData\Local\NVIDIA 
*\AppData\Local\Temp 
*\AppData\LocalLow\Google

(all those have a million tiny cache files that take lots of time to parse but aren't needed for restore; most of the incremental backup run time is processing these)

The path prior to "\AppData..." varies, depending on the machine name, user name (multiple users per machine) and folder structure on that particular machine (not everyone has all their data on c:).

To keep things simple, I've got an environment variable called _exclude that I use in each call to Robocoy:

SET _exclude= /XD *AppData\Local\Google *AppData\Local\Temp [...etc]

I don't want to exclude every directory that contains "AppData" or "Local" or "Google", as that would exclude valuable data. Just those particular sub-paths.

I've tried it with and without leading '\', with and without double quotes around the paths, with/without '*', and permutations of those.

Is there any way to make this work with Robocopy, without hard-coding the particular path for every user on every machine?

社区高分解答

原帖链接 · Evan Anderson · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-11-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。