【问题标题】:Excel VBA copy folder, move all files - but if file exists skipExcel VBA 复制文件夹,移动所有文件 - 但如果文件存在跳过
【发布时间】:2021-04-01 09:16:42
【问题描述】:

我正在尝试使用 fso.folder 副本在网络驱动器上创建备份数据库。我的意图是移动文件夹中的所有文件,但如果备份驱动器上已经存在文件,请跳过它,然后复制文件夹中的其余文件。我目前有

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

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

但是,脚本在找到现有文件时出错。任何建议将不胜感激。

【问题讨论】:

    标签: vba database file fso


    【解决方案1】:

    复制文件而不覆盖

    • 我会推荐第一个解决方案。 documentation 是“有点引导你”(至少是我)使用第二种解决方案。由您决定是否第二个可能更有效。您不能在folder part 上申请On Error

    守则

    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
    

    【讨论】:

    • 感谢您的帮助!第一个是最感性的。有没有办法在文件夹存在路径中添加一个循环来复制所有子文件夹并在其中检查?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2018-10-13
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多