CopyFile method
Copy one or more files from one location to another.
object.CopyFile source, destination[, overwrite]
Parameters
Object
Required option. Should be the name of the FileSystemObject object.
Source
Required option. A string representing the specified file. When you copy one or more files, you can have wildcard characters in the file name.
Destination
Required option. A string representing the destination location from which to copy the file. Wildcard characters are not allowed.
Overwrite
Options available. A Boolean value indicates whether the existing file is overwritten. If True, overwrites the file or, if False, does not overwrite the existing file. The default value is True. Note that theCopyFile operation cannot be completed, regardless of the value of the overwrite setting, as long as the destination is set to read-only.
Description
Wildcard characters can only be used in the last part of the path of the source parameter. For example, you can use:
FileSystemObject.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"
However, you cannot use:
FileSystemObject.CopyFile "c:\mydocuments\*\R1???97.xls", "c:\tempfolder"
If source contains wildcards or destination ends with a path delimiter (\), assume that destination is an existing folder and copy the matching file to that folder. Otherwise, assume that destination is the file you want to create. In either case, the following three situations occur when you copy a single file.
- If destination does not exist, copy source. This is usually what happens.
- If destination is a file that already exists, an error occurs when overwrite is False . Otherwise, the copy source overwrites the existing file.
- If the destination is a directory, an error occurs.
If source uses wildcards, but there are no matching files, an error occurs. The CopyFile method stops when it encounters the first error that occurs. This method does not undo any changes made before the error occurred.