robocopy

将文件数据从一个位置复制到另一个位置。

Syntax

robocopy <source> <destination> [<file>[ ...]] [<options>]

For example, to copy a file named yearly-report.mov from c:\reports to a file share \\marketing\videos while enabling multi-threading for higher performance (with the /mt parameter) and the ability to restart the transfer in case it's interrupted (with the /z parameter), type:

robocopy c:\reports "\\marketing\videos" yearly-report.mov /mt /z

Important

If any data is copied from the root of a device, the destination directory adopts the "hidden" and "system" attributes during the copy process.

Parameters

Parameter Description
<source> 指定源目录的路径。
<destination> 指定目标目录的路径。
<file> 指定要复制的一个或多个文件。 Wildcard characters (* or ?) are supported. 如果未指定此参数,*.* 将用作默认值。
<options> Specifies the options to use with the robocopy command, including copy, file, retry, logging, and job options.

Copy options

Option Description
/s Copies subdirectories. 此选项自动排除空目录。
/e Copies subdirectories. 此选项自动包括空目录。
/lev:<n> Copies only the top n levels of the source directory tree.
/z 以可重启模式复制文件。 在可重启模式下,如果文件复制中断,robocopy 可以从中断位置继续,无需重新复制整个文件。
/b 以备份模式复制文件。 在备份模式下,robocopy 会替代文件和文件夹权限设置 (ACL),后者在未被替代的情况下可能会阻止访问。
/zb 以可重启模式复制文件。 如果文件访问被拒绝,则切换到备份模式。
/j 使用无缓冲 I/O 进行复制(建议用于大文件)。
/efsraw 在 EFS RAW 模式下复制所有已加密的文件。
/copy:<copyflags> 指定要复制的文件属性。 此选项的有效值为:
  • D - Data
  • A - Attributes
  • T - Time stamps
  • X - Skip alt data streams
  • S - NTFS access control list (ACL)
  • O - Owner information
  • U - Auditing information
The default value for the /COPY option is DAT (data, attributes, and time stamps). The X flag is ignored if either /B or /ZB is used.
/dcopy:<copyflags> 指定要在目录中复制的内容。 此选项的有效值为:
  • D - Data
  • A - Attributes
  • T - Time stamps
  • E - Extended attribute
  • X - Skip alt data streams
The default value for this option is DA (data and attributes).
/sec Copies files with security (equivalent to /copy:DATS).
/copyall Copies all file information (equivalent to /copy:DATSOU).
/nocopy Copies no file information (useful with /purge).
/secfix 修复所有文件的文件安全性,甚至包括已跳过的文件。
/timfix 修复所有文件的文件时间,甚至包括已跳过的文件。
/purge 删除源中不再存在的目标文件和目录。 Using this option with the /e option and a destination directory, allows the destination directory security settings to not be overwritten.
/mir Mirrors a directory tree (equivalent to /e plus /purge). Using this option with the /e option and a destination directory, overwrites the destination directory security settings.
/mov 移动文件,并在复制文件后从源中将其删除。
/move 移动文件和目录,并在复制这些文件和目录后从源中将其删除。
/a+:[RASHCNET] 将指定的属性添加到复制的文件。 此选项的有效值为:
  • R - Read only
  • A - Archive
  • S - System
  • H - Hidden
  • C - Compressed
  • N - Not content indexed
  • E - Encrypted
  • T - Temporary
/a-:[RASHCNETO] 从复制的文件中删除指定的属性。 此选项的有效值为:
  • R - Read only
  • A - Archive
  • S - System
  • H - Hidden
  • C - Compressed
  • N - Not content indexed
  • E - Encrypted
  • T - Temporary
  • O - Offline
/create 仅创建目录树和零长度文件。
/fat 仅使用 8.3 字符长度 FAT 文件名创建目标文件。
/256 关闭对超过 256 个字符的路径的支持。
/mon:<n> Monitors the source and runs again when more than n changes are detected.
/mot:<m> Monitors the source and runs again in m minutes if changes are detected.
/rh:hhmm-hhmm 指定可以启动新副本的运行时间。
/pf 检查每个文件(不是每个阶段)的运行时间。
/ipg:<n> 指定数据包间的间隙,以便在慢速线路上释放带宽。
/sj 将交接点(软链接)复制到目标路径而不是链接目标。
/sl 不是接在符号链接的后面,而是创建链接的副本。
/mt:<n> Creates multi-threaded copies with n threads. n must be an integer between 1 and 128. The default value for n is 8. For better performance, redirect your output using /log option.

The /mt parameter can't be used with the /ipg and /efsraw parameters.

/nodcopy Copies no directory info (the default /dcopy:DA is done).
/nooffload 在不使用 Windows 复制/卸载机制的情况下复制文件。
/compress 在文件传输期间请求网络压缩(如果适用)。
/sparse:<y|n> 启用或禁用在复制过程期间保留文件的稀疏状态。 If no option is selected, it defaults to yes (enabled).
/noclone 不会尝试将区块克隆作为一种优化。

Important

When using the /secfix copy option, specify the type of security information you want to copy, using one of these additional copy options:

  • /copyall
  • /copy:o
  • /copy:s
  • /copy:u
  • /sec

Note

The /mt parameter was introduced in Windows Server 2008 R2 and its functionality applies to current versions of Windows Server.

复制文件限制选项

Option Description
/iomaxsize:<n>[kmg] The requested max i/o size per read/write cycle in nkilobytes, megabytes, or gigabytes.
/iorate:<n>[kmg] The requested i/o rate in nkilobytes megabytes, or gigabytes per second.
/threshold:<n>[kmg] The file size threshold for throttling in nkilobytes, megabytes, or gigabytes (see Remarks).

这些限制选项可用于指定 Robocopy 允许使用的最大 I/O 带宽(以“字节/秒”为单位)。 If not specifying in bytes per second, whole numbers can be used if k, m, or g are specified. The minimum I/O bandwidth that is throttled is 524288 bytes even if a lesser value is specified.

文件选择选项

Option Description
/a Copies only files for which the Archive attribute is set.
/m Copies only files for which the Archive attribute is set, and resets the Archive attribute.
/ia:[RASHCNETO] 仅包括设置了任何指定属性的文件。 此选项的有效值为:
  • R - Read only
  • A - Archive
  • S - System
  • H - Hidden
  • C - Compressed
  • N - Not content indexed
  • E - Encrypted
  • T - Temporary
  • O - Offline
/xa:[RASHCNETO] 排除设置了任何指定属性的文件。 此选项的有效值为:
  • R - Read only
  • A - Archive
  • S - System
  • H - Hidden
  • C - Compressed
  • N - Not content indexed
  • E - Encrypted
  • T - Temporary
  • O - Offline
/xf <filename>[ ...] 排除与指定的名称或路径匹配的文件。 Wildcard characters (* and ?) are supported.
/xd <directory>[ ...] 排除与指定的名称和路径匹配的目录。
/xc 排除时间戳相同但文件大小不同的现有文件。
/xn 从复制中排除比目标更新的源目录文件。
/xo 从复制中排除比目标更旧的源目录文件。
/xx 排除目标(而不是源)中存在的额外文件和目录。 排除额外文件不会从目标中删除文件。
/xl 排除源(而不是目标)中存在的“孤立”文件和目录。 排除孤立的文件可防止将任何新文件添加到目标。
/im 包括已修改的文件(更改时间不同)。
/is 包括相同的文件。 相同文件的名称、大小、时间和所有属性都相同。
/it 包括“已调整”的文件。 已调整文件的名称、大小和时间相同,但属性不同。
/max:<n> Specifies the maximum file size (to exclude files bigger than n bytes).
/min:<n> Specifies the minimum file size (to exclude files smaller than n bytes).
/maxage:<n> Specifies the maximum file age to exclude files older than n days or a date based on when the files were last modified.
/minage:<n> Specifies the minimum file age to exclude files newer than n days or a date based on when the files were last modified.
/maxlad:<n> Specifies the maximum last access date (excludes files unused since n).
/minlad:<n> Specifies the minimum last access date (excludes files used since n) If n is less than 1900, n specifies the number of days. Otherwise, n specifies a date in the format YYYYMMDD.
/xj 排除交接点(通常默认会包含)。
/fft 采用 FAT 文件时间(精度为两秒)。
/dst 补偿一小时 DST 时差。
/xjd 排除目录的交接点。
/xjf 排除文件的交接点。

Retry options

Option Description
/r:<n> 指定复制失败时的重试次数。 The default value of n is 1,000,000 (one million retries).
/w:<n> 指定等待重试的间隔时间,以秒为单位。 The default value of n is 30 (wait time 30 seconds).
/reg Saves the values specified in the /r and /w options as default settings in the registry.
/tbd 指定系统等待定义共享名称(重试错误 67)。
/lfsm Operate in low free space mode that enables copy, pause, and resume (see Remarks).
/lfsm:<n>[kmg] Specifies the floor size in nkilobytes, megabytes, or gigabytes.

Logging options

Option Description
/l 指定仅列出文件(而不复制、删除或设置时间戳)。
/x 报告所有额外的文件,而不仅仅是已选择的文件。
/v 生成详细输出,并显示所有已跳过的文件。
/ts 在输出中包括源文件时间戳。
/fp 在输出中包括文件的完整路径名称。
/bytes 以字节为单位输出大小。
/ns 指定不记录文件大小。
/nc 指定不记录文件类。
/nfl 指定不记录文件名。
/ndl 指定不记录目录名称。
/np 指定不显示复制作的进度(到目前为止复制的文件数或目录数)。
/eta 显示复制的文件的估计到达时间 (ETA)。
/log:<logfile> 将状态输出写入到日志文件(覆盖现有的日志文件)。
/log+:<logfile> 将状态输出写入日志文件(将输出追加到现有日志文件)。
/unilog:<logfile> 将状态输出作为 Unicode 文本写入日志文件(覆盖现有日志文件)。
/unilog+:<logfile> 将状态输出作为 Unicode 文本写入日志文件(将输出追加到现有日志文件)。
/tee 将状态输出写入控制台窗口和日志文件。
/njh 指定没有作业标头。
/njs 指定没有作业摘要。
/unicode 将状态输出显示为 Unicode 文本。

Job options

Option Description
/job:<jobname> 指定要从命名的作业文件派生参数。 若要运行 /job:jobname,必须首先运行 /save:jobname 参数来创建作业文件。
/save:<jobname> 指定要将参数保存到命名的作业文件。 此参数必须在运行 /job:jobname 之前运行。 必须在此参数之前指定所有复制、重试和日志记录选项。
/quit 处理命令行后退出(以查看参数)。
/nosd 指示未指定任何源目录。
/nodd 指示未指定目标目录。
/if 包括指定的文件。

Remarks

  • Using /PURGE or /MIR on the root directory of the volume formerly caused robocopy to apply the requested operation on files inside the System Volume Information directory as well. 这不再如此,就像指定了任一类型一样,robocopy 会跳过复制会话的顶级源目录和目标目录中具有该名称的任何文件或目录。

  • 仅当源和目标文件系统支持更改时间戳(例如 NTFS)并且源和目标文件具有不同的更改时间但其他方面都相同时,修改的文件分类才适用。 默认不会复制这些文件。 Specify /IM to include them.

  • The /DCOPY:E flag requests that extended attribute copying should be attempted for directories. Robocopy 继续执行复制作,即使无法复制目录的 EA。 This flag isn't included in /COPYALL.

  • If either /IoMaxSize or /IoRate are specified, robocopy enables copy file throttling to reduce system load. 可以对两者进行调整来优化值和复制参数,但允许系统和 robocopy 根据需要将它们调整为允许的值。

  • If /Threshold is used, it specifies a minimum file size for engaging throttling. 低于该大小的文件不受限制。 所有三个参数的值均可后跟一个可选后缀字符,例如 [KMG](千字节、兆字节、千兆字节)。

  • Using /LFSM requests robocopy to operate in 'low free space mode'. 在此模式下,每当文件副本会导致目标卷的可用空间低于“floor”值时,robocopy 都会暂停。 This value can be explicitly specified using /LFSM:n[KMG] flag.

  • If /LFSM is specified with no explicit floor value, the floor is set to 10% of the destination volume's size. Low free space mode is incompatible with /MT and /EFSRAW.

退出(返回)代码

Value Description
0 未复制任何文件。 未遇到任何失败。 没有不匹配的文件。 目标目录中已存在这些文件;因此跳过了复制操作。
1 已成功复制所有文件。
2 目标目录中的其他一些文件在源目录中不存在。 未复制任何文件。
3 复制了一些文件。 存在其他文件。 未遇到任何失败。
5 复制了一些文件。 某些文件不匹配。 未遇到任何失败。
6 存在其他文件和不匹配的文件。 未复制任何文件,也没有遇到任何故障,这意味着文件已存在于目标目录中。
7 已复制文件,存在文件不匹配情况,并且存在其他文件。
8 有多个文件未复制。

Note

Any value equal to or greater than 8 indicates that there was at least one failure during the copy operation.

Examples

强烈建议在运行 robocopy 命令时创建一个日志文件,该日志文件可在进程完成验证其完整性后查看。 在以下示例中,每一个都使用 /LOG: 参数。 若要将任何日志信息追加到同一日志文件,请改用 /LOG+: 参数。

若要将所有文件和子目录(包括空目录)从驱动器“D”上的“记录”文件夹复制到“备份”文件夹,请键入:

robocopy C:\Users\Admin\Records D:\Backup /E /ZB /LOG:C:\Logs\Backup.log

若要将“记录”文件夹的内容镜像到驱动器“D”上的“备份”文件夹,请删除源中不存在的任何文件,并在每次重试之间等待 5 秒,键入:

robocopy C:\Users\Admin\Records D:\Backup /MIR /R:2 /W:5 /LOG:C:\Logs\Backup.log

要将所有不为空的文件和子目录从驱动器“D”上的“备份”文件夹复制到驱动器“D”上的“备份”文件夹,请保留具有 16 个多线程复制作的文件数据、属性和时间戳,请键入:

robocopy C:\Users\Admin\Records D:\Backup /S /E /COPY:DAT /MT:16 /LOG:C:\Logs\Backup.log

若要将文件和子目录(不包括空目录)从“记录”文件夹移动到驱动器“D”上的“备份”文件夹,并排除超过 7 天的文件,请键入:

robocopy C:\Users\Admin\Records D:\Backup /S /MAXAGE:7 /MOV /LOG:C:\Logs\Backup.log

若要将所有文件和子目录(包括空目录)从驱动器“D”上的“记录”文件夹复制到驱动器“D”上的“备份”文件夹,显示每个文件的估计时间,并删除源中不存在的任何文件和目录,请键入:

robocopy C:\Users\Admin\Records D:\Backup /ETA /PURGE /LOG:C:\Logs\Backup.log

若要将“C”驱动器上名为“Records”的文件夹中的所有文件和子目录复制到“D”驱动器上的名为“Backup”的文件夹,同时在复制作期间将 I/O 速率限制为每秒 1 兆字节,请键入:

robocopy C:\Records D:\Backup /iorate:1m

若要在目标文件夹中已存在文件时跳过将文件从源文件夹复制到目标文件夹,无论这些文件是较新、较旧还是已修改,请键入:

robocopy C:\Source C:\Destination /XC /XN /XO