如何使用 xcopy 排除某些文件被复制?

How to exclude some files from being copied using xcopy?

我正在尝试将文件夹从一个地方复制到网上邻居。但是我想排除一些文件夹被复制。

:: time and date stamp YYYYMMDD, HHMMSS and YYYY-MM-DD_HH-MM-SS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%

set stamp=%YYYY%-%MM%-%DD%T%HH%-%Min%-%Sec%
mkdir "\serv1\b$\Script Backup\%stamp%"
XCOPY C:\source "\serv1\b$\Script Backup\%stamp%" /S

此 xcopy 命令会将整个 C:\source 复制到网络位置。但我想排除当前位于 C:\source

中的以下目录

我想排除以下文件夹

C:\source\dba
C:\source\staging
C:\source\testing

我该怎么做?

根据Product Documentation - Xcopy

/exclude:filename1[+[filename2]][+[filename3]] : Specifies a list of files containing strings.

和(添加了重点):

List each string in a separate line in each file. If any of the listed strings match any part of the absolute path of the file to be copied, that file is then excluded from the copying process. For example, if you specify the string "\Obj\", you exclude all files underneath the Obj directory. If you specify the string ".obj", you exclude all files with the .obj extension.

因此,创建一个文件(例如 exclude.txt)以包含:

C:\source\dba
C:\source\staging
C:\source\testing

并指示 /exclude: 参数引用它:

XCOPY C:\source "\serv1\b$\Script Backup\%stamp%" /S /exclude:exclude.txt