FileSystemObject.CopyFile Method (VB6)

Named Arguments

Yes

Syntax

oFileSysObj.CopyFile Source, Destination [, OverwriteFiles]

oFileSysObj

Use: Required

Data Type: FileSystemObject object

Any object variable returning a FileSystemObject object.

Source

Use: Required

Data Type: String

The path and name of the file to be copied.

Destination

Use: Required

Data Type: String

The path and optionally the filename of the copy to make.

OverwriteFiles

Use: Optional

Data Type: Boolean

Flag indicating whether an existing file is to be overwritten (True) or not (False).

Description

Copies a file or files from one folder to another.

Rules at a Glance

  • The default value for OverwriteFiles is True.

  • The source path can be relative or absolute.

  • The source filename can contain wildcard characters; the source path can't.

  • Wildcard characters can't be included in Destination.

Programming Tips and Gotchas

  • If the destination path or file is read-only, the CopyFile method fails, regardless of the value of OverwriteFiles.

  • If OverwriteFiles is set to False and the file exists in Destination, a trappable error—runtime error 58, "File Already Exists"—is generated.

  • If an error occurs while copying more than one file, the CopyFile function exits immediately, thereby leaving the rest of the files uncopied. There is no roll-back facility to undo copies made prior to the error.

    If the user has adequate rights, the source or destination can be a network path or share name. For example:

    CopyFile "c:\Rootone\*.*", "\\NTSERV1\d$\RootTwo\" ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.