CopyFile 方法CopyFile method

将一个或多个文件从一个位置复制到另一个位置。Copies one or more files from one location to another.

语法Syntax

对象CopyFile目标、[覆盖]object.CopyFile source, destination, [ overwrite ]


CopyFile 方法的语法包含以下三部分:The CopyFile method syntax has these parts:

部分Part 说明Description
objectobject 必需。Required. _对象_始终是**FileSystemObject** 的名称。The object is always the name of a FileSystemObject.
来源source 必需。Required. 字符串文件规范,可以包括通配符来代表要复制的一个或多个文件。Character string file specification, which can include wildcard characters, for one or more files to be copied.
目标destination 必需。Required. 要将 source 中的文件复制到的字符串目标。Character string destination where the file or files from source are to be copied. 不能使用通配符。Wildcard characters are not allowed.
是否overwrite 可选。Optional. 布尔值,指示是否覆盖现有文件。Boolean value that indicates if existing files are to be overwritten. 如果为 True,则覆盖文件,如果为 False,则不覆盖文件。If True, files are overwritten; if False, they are not. 默认为 TrueThe default is True. 请注意,如果 destination 具有只读属性集,则不管 overwrite 值是什么,CopyFile 都将失败。Note that CopyFile will fail if destination has the read-only attribute set, regardless of the value of overwrite.

注解Remarks

通配符只能在 source 参数的最后路径组件中使用。Wildcard characters can only be used in the last path component of the source argument. 例如,可以使用:For example, you can use:

FileSystemObject.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"

但不能使用:But you can't use:

FileSystemObject.CopyFile "c:\mydocuments\*\R1???97.xls", "c:\tempfolder"

如果_source_包含通配符, 或_目标_以路径分隔符 (**) 结尾, 则假定_destination_是要在其中复制匹配文件的现有文件夹。If source contains wildcard characters, or destination ends with a path separator (**), it is assumed that destination is an existing folder in which to copy matching files. 否则,destination 被认为是要创建的文件的名称。Otherwise, destination is assumed to be the name of a file to create. 在这两种情况下, 复制单个文件时可能会发生以下三种情况:In either case, three things can happen when an individual file is copied:

  • 如果 destination 不存在,则复制 sourceIf destination does not exist, source gets copied. 这是常见情况。This is the usual case.

  • 如果 destination 是现有文件,并且 overwriteFalse,则会出现错误。If destination is an existing file, an error occurs if overwrite is False. 否则,将尝试复制 source 并覆盖现有文件。Otherwise, an attempt is made to copy source over the existing file.

  • 如果 destination 是一个目录,则会出现错误。If destination is a directory, an error occurs.

如果使用通配符的 source 与任何文件不匹配,也会出现错误。An error also occurs if a source using wildcard characters doesn't match any files. CopyFile 方法将在所遇到的第一个错误处停止。The CopyFile method stops on the first error it encounters. 在发生错误之前,不会尝试回滚或撤消任何已进行的更改。No attempt is made to roll back or undo any changes made before an error occurs.

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.