1

I am trying to create a backup database on a network drive using fso.folder copy. My intention is to move all files within the folder, but if a file already exists on the backup drive, skip it, and copy the remainder of the files in the folder. I currently have

SourceFileName="C:\users\desktop\test1"
DestinFileName="C:\users\desktop\test2"

FSO.copyfolder Source:=Sourcefilename, Destination:=Destinfilename, OverwriteFiles:= False

However, the script errors when it finds the existing file. Any advice would be appreciated.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125

1 Answers1

0

Copy Files Without Overwriting

  • I would recommend the first solution. The documentation is 'somewhat leading you on' (at least me) to use the second solution. It's up to you to find out if the second one is maybe more efficient. You cannot apply On Error on the folder part.

The Code

Option Explicit

Sub copyFilesNoOverwrite()
    
    Const srcFolderPath As String = "C:\users\desktop\test1"
    Const dstFolderPath As String = "C:\users\desktop\test2"
    
    With CreateObject("Scripting.FileSystemObject")
        If Not .FolderExists(srcFolderPath) Then
            MsgBox "Source Folder doesn't exist.", vbCritical, "No Source"
            Exit Sub
        End If
        If .FolderExists(dstFolderPath) Then
            Dim Sep As String: Sep = Application.PathSeparator
            Dim fsoFile As Object
            Dim FilePath As String
            For Each fsoFile In .GetFolder(srcFolderPath).Files
                FilePath = dstFolderPath & Sep & fsoFile.Name
                If Not .FileExists(FilePath) Then
                    .CopyFile _
                        Source:=fsoFile.Path, _
                        Destination:=FilePath
                End If
            Next fsoFile
        Else
            .CopyFolder _
                Source:=srcFolderPath, _
                Destination:=dstFolderPath
        End If
    End With

End Sub

Sub copyFilesNoOverwriteOnError()
    
    Const srcFolderPath As String = "C:\users\desktop\test1"
    Const dstFolderPath As String = "C:\users\desktop\test2"
    
    With CreateObject("Scripting.FileSystemObject")
        If Not .FolderExists(srcFolderPath) Then
            MsgBox "Source Folder doesn't exist.", vbCritical, "No Source"
            Exit Sub
        End If
        If .FolderExists(dstFolderPath) Then
            Dim Sep As String: Sep = Application.PathSeparator
            Dim fsoFile As Object
            For Each fsoFile In .GetFolder(srcFolderPath).Files
                On Error Resume Next
                .CopyFile _
                    Source:=fsoFile.Path, _
                    Destination:=dstFolderPath & Sep & fsoFile.Name, _
                    OverwriteFiles:=False
                On Error GoTo 0
            Next fsoFile
        Else
            .CopyFolder _
                Source:=srcFolderPath, _
                Destination:=dstFolderPath
        End If
    End With

End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
  • Thanks for the help! The first one is the most sensical. Is there a way to add a loop within the Folder exists path to copy all subfolders and check within those as well? – mphillies2008 Dec 22 '20 at 18:35