CopyFile 方法CopyFile method

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

语法Syntax

对象CopyFiledestination、 [覆盖]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.
sourcesource 必需。Required. 字符字符串文件规范,其中可以包括通配符字符,要复制的一个或多个文件。Character string file specification, which can include wildcard characters, for one or more files to be copied.
目标destination 必需。Required. 要在其中_源_中的文件的复制字符字符串目标。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. 请注意,是否_目标_具有只读属性设置,而不考虑的_覆盖_值,则CopyFile将会失败。Note that CopyFile will fail if destination has the read-only attribute set, regardless of the value of overwrite.

说明Remarks

仅可以_源_参数的最后一个路径组件中使用通配符。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"

如果_源_包含通配符字符或_目标_结束路径分隔符 (**),则假定其值的_目标_是用来将匹配的文件复制现有文件夹。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. 否则,_目标_被假定为要创建的文件的名称。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:

  • 如果不存在_目标_,获取复制_源_。If destination does not exist, source gets copied. 这是通常情况。This is the usual case.

  • 如果_destination_是现有文件,如果_overwrite_为False,就会出错。If destination is an existing file, an error occurs if overwrite is False. 否则,尝试将_源_复制覆盖现有的文件。Otherwise, an attempt is made to copy source over the existing file.

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

如果使用通配符_源_不匹配的任何文件,也将发生错误。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