复制文件和目录,包括子目录。
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] [/compress]
Parameters
Parameter | Description |
---|---|
<源> | Required. 指定要复制的文件的位置和名称。 此参数必须包括驱动器或路径。 |
[<目标>] | 指定要复制的文件的目标。 此参数可以包括驱动器号和冒号、目录名称、文件名或它们的组合。 |
/w | 显示以下消息并等待响应,然后再开始复制文件: 按任意键开始复制文件 |
/p | 提示你确认是否要创建每个目标文件。 |
/c | Ignores errors. |
/v | 验证每个文件是否写入目标文件,以确保目标文件与源文件相同。 |
/q | 禁止显示 xcopy 消息。 |
/f | 复制时显示源和目标文件名。 |
/l | 生成要复制但不会主动复制文件的文件列表。 |
/g | Creates decrypted destination files when the destination doesn't support encryption. |
/d [:MM-DD-YYYY] | 仅复制指定日期或之后更改的源文件。 If you don't include a MM-DD-YYYY value, xcopy copies all source files that are newer than existing destination files. 此命令行选项允许更新已更改的文件。 |
/u | Copies files from source that exist on destination only. |
/i | If source is a directory or contains wildcards and destination doesn't exist, xcopy assumes destination specifies a directory name and creates a new directory. 然后, xcopy 将所有指定的文件复制到新目录中。 By default, xcopy prompts you to specify whether destination is a file or a directory. |
/s | 复制目录和子目录,除非它们为空。 If you omit /s, xcopy works within a single directory. |
/e | 复制所有子目录,即使它们为空。 Use /e with the /s and /t command-line options. |
/t | 仅复制子目录结构(即树),而不复制文件。 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 删除只读属性。 |
/r | 复制只读文件。 |
/h | 复制具有隐藏文件和系统文件属性的文件。 默认情况下, xcopy 不会复制隐藏文件或系统文件 |
/a | Copies only source files that have their archive file attributes set. /a doesn't modify the archive file attribute of the source file. For information about how to set the archive file attribute by using attrib, see Related links. |
/m | Copies source files that have their archive file attributes set. Unlike /a, /m turns off archive file attributes in the files that are specified in the source. For information about how to set the archive file attribute by using attrib, see Related links. |
/n | 使用 NTFS 短文件或目录名称创建副本。 /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. The destination file system can be FAT or NTFS. |
/o | 复制文件所有权和自由访问控制列表(DACL)信息。 |
/x | Copies file audit settings and system access control list (SACL) information (implies /o). |
/exclude:FileName1[+[FileName2]][+[FileName3]( )] | 指定文件列表。 必须至少指定一个文件。 每个文件都包含搜索字符串,每个字符串都位于文件中的单独行上。 当任一字符串与要复制的文件的绝对路径的任何部分匹配时,将从复制中排除该文件。 For example, specifying the string obj will exclude all files underneath the directory obj or all files with the .obj extension. |
/y | 取消提示以确认要覆盖现有目标文件。 |
/-y | 提示确认是否要覆盖现有目标文件。 |
/z | 在可重启模式下通过网络复制。 |
/b | 复制符号链接而不是文件。 此参数是在 Windows Vista® 中引入的。 |
/j | 在不缓冲的情况下复制文件。 建议用于非常大的文件。 此参数已在 Windows Server 2008 R2 中添加。 |
/compress | 在文件传输期间请求网络压缩(如果适用)。 |
/[- ]sparse |
启用或禁用在复制过程期间保留文件的稀疏状态。 If both parameters are specified, /-sparse overrides /sparse. |
/noclone | 不会尝试将区块克隆作为一种优化。 |
/? | 在命令提示符下显示帮助。 |
Remarks
Using /z
如果在复制阶段失去连接(例如,如果服务器脱机断开连接),则会在重新建立连接后恢复。 /z also displays the percentage of the copy operation completed for each file.
Using /y in the COPYCMD environment variable.
You can use /y in the COPYCMD environment variable. You can override this command by using /-y on the command line. 默认情况下,系统会提示你覆盖。
复制加密的文件
将加密文件复制到不支持 EFS 的卷会导致错误。 首先解密文件,或将文件复制到支持 EFS 的卷。
Appending files
若要追加文件,请为目标指定单个文件,但源的多个文件(即,使用通配符或 file1+file2+file3 格式)。
Default value for destination
If you omit destination, the
xcopy
command copies the files to the current directory.Specifying whether destination is a file or directory
If destination doesn't contain an existing directory and doesn't 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。 如果希望将文件或文件复制到目录,请按 D。
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.Using the
xcopy
command to set archive attribute for destination files该
xcopy
命令使用存档属性集创建文件,无论此属性是否在源文件中设置。 For more information about file attributes and attrib, see Related links.比较
xcopy
和diskcopy
如果磁盘包含子目录中的文件,并且想要将其复制到具有不同格式的磁盘,请使用
xcopy
该命令而不是diskcopy
。diskcopy
由于命令按跟踪复制磁盘,因此源磁盘和目标磁盘的格式必须相同。 该xcopy
命令没有此要求。 除非需要完整的磁盘映像副本,否则请使用xcopy
。内存不足错误
如果运行
xcopy
复制文件名路径大于 255 个字符的文件或文件夹,则可能会出现“内存不足”错误。退出代码
xcopy
To process exit codes returned by
xcopy
, use the ErrorLevel parameter on the if command line in a batch program. For an example of a batch program that processes exit codes using if, see Related links. 下表列出了每个退出代码和说明。Exit code Description 0 文件已复制,且未出错。 1 找不到要复制的文件。 2 用户按 Ctrl+C 终止 xcopy
。4 出现初始化错误。 内存或磁盘空间不足,或者你在命令行上输入了无效的驱动器名称或语法无效。 5 发生磁盘写入错误。
Examples
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. 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. 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. To update all the files that exist in \Reports in the previous example, regardless of date, type:
xcopy \rawdata \reports /u
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.out 列出了要复制的每个文件。
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. To issue the previous command, ensure that xcopy
creates the \Address directory if it doesn't 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. 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
:
@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,请键入:
copyit c:\prgmcode b:
The command interpreter substitutes C:\Prgmcode for %1 and B: for %2, then uses xcopy
with the /e and /s command-line options. 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. This example copies all the non-empty directories, plus files with the associated file extension after 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
In the preceding example, this particular source parameter value .\toc*.yml copies the same 3 files even if its two path characters .\ were removed. However, no files would be copied if the asterisk wildcard was removed from the source parameter, making it just .\toc.yml.