xcopyxcopy

复制文件和目录,包括子目录。Copies files and directories, including subdirectories.

有关如何使用此命令的示例,请参阅示例For examples of how to use this command, see Examples.

语法Syntax

Xcopy <Source> [<Destination>] [/w] [/p] [/c] [/v] [/q] [/f] [/l] [/g] [/d [:MM-DD-YYYY]] [/u] [/i] [/s [/e]] [/t] [/k] [/r] [/h] [{/a | /m}] [/n] [/o] [/x] [/exclude:FileName1[+[FileName2]][+[FileName3]] [{/y | /-y}] [/z] [/b] [/j]

参数Parameters

参数Parameter 说明Description
<Source> 必需。Required. 指定要复制的文件的位置和名称。Specifies the location and names of the files you want to copy. 此参数必须包含驱动器或路径。This parameter must include either a drive or a path.
[<Destination>][<Destination>] 指定要复制的文件的目标。Specifies the destination of the files you want to copy. 此参数可以包含驱动器号和冒号、目录名称、文件名或它们的组合。This parameter can include a drive letter and colon, a directory name, a file name, or a combination of these.
/W/w 显示以下消息,并在开始复制文件之前等待你的响应:Displays the following message and waits for your response before starting to copy files:
按任意键开始复制文件 ()Press any key to begin copying file(s)
/p/p 提示您确认是否要创建每个目标文件。Prompts you to confirm whether you want to create each destination file.
/c/c 忽略错误。Ignores errors.
/v/v 在将每个文件写入目标文件时对其进行验证,以确保目标文件与源文件完全相同。Verifies each file as it is written to the destination file to make sure that the destination files are identical to the source files.
/q/q 禁止显示 xcopy 消息。Suppresses the display of xcopy messages.
/f/f 复制时显示源和目标文件名。Displays source and destination file names while copying.
/l/l 显示要复制的文件的列表。Displays a list of files that are to be copied.
/g/g 如果目标不支持加密,则创建已解密的 目标 文件。Creates decrypted Destination files when the destination does not support encryption.
/d [: MM-DD]/d [:MM-DD-YYYY] 仅复制在指定日期或指定日期之后更改的源文件。Copies source files changed on or after the specified date only. 如果不包括 MM DD 值,则 xcopy 会复制比现有 目标 文件新的所有 源文件If you do not include a MM-DD-YYYY value, xcopy copies all Source files that are newer than existing Destination files. 使用此命令行选项,可以更新已更改的文件。This command-line option allows you to update files that have changed.
/U/u 仅**从源 副本复制文件。Copies files from Source that exist on Destination only.
/i/i 如果 是一个目录或包含通配符并且 目标 不存在,则 xcopy 假设 destination 指定目录名称并创建一个新目录。If Source is a directory or contains wildcards and Destination does not exist, xcopy assumes Destination specifies a directory name and creates a new directory. 然后, xcopy 将所有指定的文件复制到新目录中。Then, xcopy copies all specified files into the new directory. 默认情况下, xcopy 会提示您指定 目标 是文件还是目录。By default, xcopy prompts you to specify whether Destination is a file or a directory.
/s/s 复制目录和子目录,除非它们为空。Copies directories and subdirectories, unless they are empty. 如果省略 /s,则 xcopy 在单个目录中工作。If you omit /s, xcopy works within a single directory.
/e/e 复制所有子目录(即使它们为空)。Copies all subdirectories, even if they are empty. 使用 /e/s/t 命令行选项。Use /e with the /s and /t command-line options.
/t /t 复制 (的子目录结构,即树) ,而不是文件。Copies the subdirectory structure (that is, the tree) only, not files. 若要复制空目录,必须包含 /e 命令行选项。To copy empty directories, you must include the /e command-line option.
遇到/k 复制文件并在 目标 文件上保留只读属性(如果存在于 源文件 上)。Copies files and retains the read-only attribute on Destination files if present on the Source files. 默认情况下, xcopy 会删除只读属性。By default, xcopy removes the read-only attribute.
/r/r 复制只读文件。Copies read-only files.
/h/h 复制具有隐藏文件和系统文件属性的文件。Copies files with hidden and system file attributes. 默认情况下, xcopy 不复制隐藏文件或系统文件By default, xcopy does not copy hidden or system files
/a/a 仅复制设置了存档文件属性的 源文件Copies only Source files that have their archive file attributes set. /a 不修改源文件的 "存档文件" 属性。/a does not modify the archive file attribute of the source file. 有关如何使用 attrib 设置存档文件属性的信息,请参阅 " 其他参考"。For information about how to set the archive file attribute by using attrib, see Additional References.
/m/m 复制设置了其存档文件属性的 源文件Copies Source files that have their archive file attributes set. /a 不同, /m 关闭源中指定的文件中的存档文件属性。Unlike /a, /m turns off archive file attributes in the files that are specified in the source. 有关如何使用 attrib 设置存档文件属性的信息,请参阅 " 其他参考"。For information about how to set the archive file attribute by using attrib, see Additional References.
/n/n 使用 NTFS 短文件名或目录名称创建副本。Creates copies by using the NTFS short file or directory names. 从 NTFS 卷向 FAT 卷复制文件或目录时,或者当 FAT 文件系统命名约定 (即,在 目标 文件系统上) 需要8.3 个字符时,需要 /n/n is required when you copy files or directories from an NTFS volume to a FAT volume or when the FAT file system naming convention (that is, 8.3 characters) is required on the Destination file system. 目标 文件系统可以是 FAT 或 NTFS。The Destination file system can be FAT or NTFS.
/o/o (DACL) 信息复制文件所有权和随机访问控制列表。Copies file ownership and discretionary access control list (DACL) information.
/x/x (SACL) 信息复制文件审核设置和系统访问控制列表, (隐含 /o) 。Copies file audit settings and system access control list (SACL) information (implies /o).
/exclude: FileName1 [+ [FileName2] [+ [FileName3] ( ) ]/exclude:FileName1[+[FileName2][+[FileName3]( )] 指定文件的列表。Specifies a list of files. 至少必须指定一个文件。At least one file must be specified. 每个文件都包含搜索字符串,每个字符串在文件中的单独一行上。Each file will contain search strings with each string on a separate line in the file.
如果任意字符串与要复制的文件的绝对路径的任何部分匹配,则将不复制该文件。When any of the strings match any part of the absolute path of the file to be copied, that file will be excluded from being copied. 例如,如果指定字符串 obj ,将排除目录 obj 下的所有文件或扩展名为 .obj 的所有文件。For example, specifying the string obj will exclude all files underneath the directory obj or all files with the .obj extension.
/y/y 禁止提示您确认是否要覆盖现有目标文件。Suppresses prompting to confirm that you want to overwrite an existing destination file.
/-y/-y 提示确认是否要覆盖现有目标文件。Prompts to confirm that you want to overwrite an existing destination file.
/z/z 在可重启模式下通过网络复制。Copies over a network in restartable mode.
/b/b 复制符号链接而不是文件。Copies the symbolic link instead of the files. 此参数是在 Windows Vista®中引入的。This parameter was introduced in Windows Vista®.
/j/j 无缓冲复制文件。Copies files without buffering. 建议用于非常大的文件。Recommended for very large files. 此参数是在 Windows Server 2008 R2 中添加的。This parameter was added in Windows Server 2008 R2.
/?/? 在命令提示符下显示帮助。Displays help at the command prompt.

备注Remarks

  • 使用 /zUsing /z

    如果在复制阶段丢失连接 (例如,如果服务器脱机时断开连接) ,则在重新建立连接后会恢复。If you lose your connection during the copy phase (for example, if the server going offline severs the connection), it resumes after you reestablish the connection. /z 还显示每个文件完成的复制操作的百分比。/z also displays the percentage of the copy operation completed for each file.

  • 在 COPYCMD 环境变量中使用 /yUsing /y in the COPYCMD environment variable.

    可以在 COPYCMD 环境变量中使用 /yYou can use /y in the COPYCMD environment variable. 可以通过在命令行上使用 /-y 来覆盖此命令。You can override this command by using /-y on the command line. 默认情况下,系统将提示您进行覆盖。By default, you are prompted to overwrite.

  • 复制加密的文件Copying encrypted files

    将加密文件复制到不支持 EFS 的卷会导致错误。Copying encrypted files to a volume that does not support EFS results in an error. 首先对文件进行解密,或将文件复制到支持 EFS 的卷。Decrypt the files first or copy the files to a volume that does support EFS.

  • 追加文件Appending files

    若要附加文件,请为目标指定单个文件,但对于源 (使用多个文件,使用通配符或 file1 + file2 + file3 格式) 。To append files, specify a single file for destination, but multiple files for source (that is, by using wildcards or file1+file2+file3 format).

  • 目标 的默认值Default value for Destination

    如果省略 Destinationxcopy 命令会将文件复制到当前目录中。If you omit Destination, the xcopy command copies the files to the current directory.

  • 指定 目标 是否为文件或目录Specifying whether Destination is a file or directory

    如果 目标 不包含现有目录且不以反斜杠 (结尾 ) ,将显示以下消息:If Destination does not contain an existing directory and does not end with a backslash (), the following message appears:

    Does <Destination> specify a file name or directory name on the target(F = file, D = directory)?
    

如果要将文件复制到文件中,请按 F。Press F if you want the file or files to be copied to a file. 如果要将文件复制到目录,请按 D。Press D if you want the file or files to be copied to a directory.

您可以通过使用 /i 命令行选项来禁止显示此消息,这会导致 xcopy 假设目标是一个目录(如果源是多个文件或目录)。You can suppress this message by using the /i command-line option, which causes xcopy to assume that the destination is a directory if the source is more than one file or a directory.

  • 使用 xcopy 命令为 目标 文件设置存档属性Using the xcopy command to set archive attribute for Destination files

    如果在源文件中设置了此属性, xcopy 命令将创建具有 archive 属性集的文件。The xcopy command creates files with the archive attribute set, whether or not this attribute was set in the source file. 有关文件 属性和属性 的详细信息,请参阅 " 其他参考"。For more information about file attributes and attrib, see Additional References.

  • 比较 xcopydiskcopyComparing xcopy and diskcopy

    如果有包含子目录中的文件的磁盘,并且想要将文件复制到具有不同格式的磁盘,请使用 xcopy 命令而不是 diskcopyIf you have a disk that contains files in subdirectories and you want to copy it to a disk that has a different format, use the xcopy command instead of diskcopy. 由于 diskcopy 命令按曲目复制磁盘曲目,因此源和目标磁盘的格式必须相同。Because the diskcopy command copies disks track by track, your source and destination disks must have the same format. Xcopy 命令没有这一要求。The xcopy command does not have this requirement. 使用 xcopy ,除非你需要完整的磁盘映像副本。Use xcopy unless you need a complete disk image copy.

  • Xcopy 的退出代码Exit codes for xcopy

    若要处理 xcopy 返回的退出代码,请在批处理程序的 if 命令行中使用 ErrorLevel 参数。To process exit codes returned by xcopy, use the ErrorLevel parameter on the if command line in a batch program. 有关使用 if 处理退出代码的批处理程序的示例,请参阅 " 其他参考"。For an example of a batch program that processes exit codes using if, see Additional References. 下表列出了每个退出代码和说明。The following table lists each exit code and a description.

    退出代码Exit code 说明Description
    00 复制文件时没有错误。Files were copied without error.
    11 找不到要复制的文件。No files were found to copy.
    22 用户按下 CTRL + C 终止了 xcopyThe user pressed CTRL+C to terminate xcopy.
    44 出现初始化错误。Initialization error occurred. 内存或磁盘空间不足,或者您在命令行中输入了无效的驱动器名称或无效语法。There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.
    55 出现磁盘写入错误。Disk write error occurred.

示例Examples

1. 若要复制所有文件和子目录 (包括从驱动器 a 到驱动器 B) 的任何空子目录,请键入:1. To copy all the files and subdirectories (including any empty subdirectories) from drive A to drive B, type:

xcopy a: b: /s /e

2. 若要包括上一示例中的任何系统文件或隐藏文件,请添加 /h 命令行选项,如下所示:2. To include any system or hidden files in the previous example, add the/h command-line option as follows:

xcopy a: b: /s /e /h

3. 若要使用自1993年12月29日以来发生更改的 \Rawdata 目录中的文件更新 \Reports 目录中的文件,请键入:3. To update files in the \Reports directory with the files in the \Rawdata directory that have changed since December 29, 1993, type:

xcopy \rawdata \reports /d:12-29-1993

4. 若要更新上一个示例中的 \Reports 中存在的所有文件,而不考虑日期,请键入:4. To update all the files that exist in \Reports in the previous example, regardless of date, type:

xcopy \rawdata \reports /u

5. 若要获取前一个命令要复制的文件的列表 (也就是说,不) 实际复制文件,请键入:5. To obtain a list of the files to be copied by the previous command (that is, without actually copying the files), type:

xcopy \rawdata \reports /d:12-29-1993 /l > xcopy.out

文件 xcopy 列出每个要复制的文件。The file xcopy.out lists every file that is to be copied.

6. 若要将 \Customer 目录和所有子目录复制到 \ \ 网络驱动器 H:上的目录 Public\Address,请保留只读属性,并且在 H:上创建新文件时,请键入:6. To copy the \Customer directory and all subdirectories to the directory \\Public\Address on network drive H:, retain the read-only attribute, and be prompted when a new file is created on H:, type:

xcopy \customer h:\public\address /s /e /k /p

7. 若要发出前面的命令,请确保 xcopy 创建 \Address 目录(如果不存在),并取消创建新目录时显示的消息,并按如下所示添加 /i 命令行选项:7. To issue the previous command, ensure that xcopy creates the \Address directory if it does not exist, and suppress the message that appears when you create a new directory, add the /i command-line option as follows:

xcopy \customer h:\public\address /s /e /k /p /i

8. 您可以创建一个批处理程序来执行 xcopy 操作,并使用 batch if 命令处理退出代码(如果出现错误)。8. You can create a batch program to perform xcopy operations and use the batch if command to process the exit code if an error occurs. 例如,以下批处理程序对 xcopy 源和目标参数使用可替换参数:For example, the following batch program uses replaceable parameters for the xcopy source and destination parameters:

@echo off
rem COPYIT.BAT transfers all files in all subdirectories of
rem the source drive or directory (%1) to the destination
rem drive or directory (%2)
xcopy %1 %2 /s /e
if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 0 goto exit
:lowmemory
echo Insufficient memory to copy files or
echo invalid drive or command-line syntax.
goto exit
:abort
echo You pressed CTRL+C to end the copy operation.
goto exit
:exit

若要使用上述批处理程序将 C:\Prgmcode 目录及其子目录中的所有文件复制到驱动器 B,请键入:To use the preceding batch program to copy all files in the C:\Prgmcode directory and its subdirectories to drive B, type:

copyit c:\prgmcode b:

命令解释器会将 %1B: 替换为 %2C:\Prgmcode并使用 /e/s 命令行选项。The command interpreter substitutes C:\Prgmcode for %1 and B: for %2, then uses xcopy with the /e and /s command-line options. 如果 xcopy 遇到错误,批处理程序将读取退出代码,并转到相应 IF ERRORLEVEL 语句中指示的标签,然后显示相应的消息并退出批处理程序。If xcopy encounters an error, the batch program reads the exit code and goes to the label indicated in the appropriate IF ERRORLEVEL statement, then displays the appropriate message and exits from the batch program.

9. 此示例复制所有非空目录,并复制其名称与星号符号给定的模式匹配的文件。9. This example copies all the non-empty directories, plus files whose name match the pattern given with the asterisk symbol.

xcopy .\toc*.yml ..\..\Copy-To\ /S /Y

rem Output example.
rem  .\d1\toc.yml
rem  .\d1\d12\toc.yml
rem  .\d2\toc.yml
rem  3 File(s) copied

在前面的示例中,此特定的源参数值 。 \即使 * 删除了两个 路径 \ 字符, docker-compose.override.yml 也会复制相同的3个文件。In the preceding example, this particular source parameter value .\toc*.yml copies the same 3 files even if its two path characters .\ were removed. 但是,如果从 source 参数中删除星号通配符,则不会复制任何文件 。 \docker-compose.override.ymlHowever, no files would be copied if the asterisk wildcard was removed from the source parameter, making it just .\toc.yml.

其他参考Additional References