File: Copy

Copy

Copies an existing file to a new file. Overwriting a file of the same name is allowed.



 Public Sub Copy(
	  ByRef SourceFileName As String,
	  ByRef DestFileName As String,
	  Optional ByVal OverWrite As Boolean = False )

Parameters

SourceFileName
[ByRef] String. The file to copy.
DestFileName
[ByRef] String. The name of the destination file. This cannot be a directory.
OverWrite
[ByVal] Optional. Boolean. True if the destination file can be overwritten; otherwise, False.  

Default: False

Remarks

The SourceFileName and DestFileName parameters can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.

Exceptions

ExceptionCondition
UnauthorizedAccessException DestFileName is read-only.
-or-
DestFileName is a directory.
ArgumentException Path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by GetInvalidPathChars.
PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. On Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
DirectoryNotFoundException The specified path is invalid (for example, it is on an unmapped drive).
NotSupportedException Path is in an invalid format.

Examples

The following example copies files to the C:\archives\2008 backup folder. It uses the two overloads of the Copy method as follows:

Public Sub Main()
    Const SourceDir As String = "c:\current"
    Const BackupDir As String = "c:\archives\2008"

    Dim PicList() As String
    Dim TxtList() As String
    Dim FName As String
    Dim f As Variant
    
    PicList = Directory.GetFiles(SourceDir, "*.jpg")
    TxtList = Directory.GetFiles(SourceDir, "*.txt")
    
    ' Copy picture files.
    For Each f In PicList
        'Remove path from the file name.
        
        FName = Path.GetFileName(CStr(f))
        
        ' Use the Path.Combine method to safely append the file name to the path.
        ' Will overwrite if the destination file already exists.
        File.Copy Path.Combine(SourceDir, FName), Path.Combine(BackupDir, FName), True
    Next

    ' Copy text files.
    For Each f In TxtList
        'Remove path from the file name.
        FName = Path.GetFileName(CStr(f))

        On Error GoTo CopyError
        ' Will not overwrite if the destination file already exists.
        File.Copy Path.Combine(SourceDir, FName), Path.Combine(BackupDir, FName)
        GoTo EndTry
        
        ' Catch exception if the file was already copied.
CopyError:
        Dim CopyError As Exception
        Catch CopyError, Err
        Debug.Print CopyError.Message
EndTry:
    Next

    For Each f In TxtList
        File.Delete CStr(f)
    Next

    For Each f In PicList
        File.Delete CStr(f)
    Next

DirNotFound:
    Dim DirNotFound As Exception
    Catch DirNotFound, Err
    Debug.Print DirNotFound.Message
End Sub

See Also

Project CorLib Overview

Class File Overview