【问题标题】:copy a folder- and all subfolders - without overwriting existing复制文件夹和所有子文件夹,而不覆盖现有的
【发布时间】:2021-04-01 12:01:10
【问题描述】:

0

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

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

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

【问题讨论】:

  • FSO 是一个 VBScript 对象。它不接受这种方式的参数(使用Destination:=Destinfilename, OverwriteFiles:= False)。 CopyFolder函数的主题是FSO.CopyFolder SourceFileName, DestinationFileName, False,其中前两个参数是字符串,意思是他们的名字,最后一个是Boolean类型,代表Overwrite。请按照我上面的建议尝试使用它并发送一些反馈
  • 谢谢 - 调整时我仍然显示文件已存在的错误。
  • 是的,从这个角度来看,VBScript 很糟糕。我将立即发布带有解决方案的答案,以初步检查该文件夹是否存在...

标签: vba database directory fso


【解决方案1】:

不覆盖的备份文件夹及其子文件夹

  • 以下内容会将源文件夹备份到目标文件夹,即复制丢失的文件夹和文件。
  • TESTcopyFolder 只是您如何使用该解决方案的一个示例。
  • 它将调用初始化过程backupFolder,必要时将调用backupFolderCopybackupFolderRecurse
  • 声明Private SkipPath As String 和三个过程必须复制到同一个(通常是标准的)模块,例如Module1

守则

Option Explicit

Private SkipPath As String

Sub TESTcopyFolder()
     
    Const srcPath As String = "F:\Test\2020\65412587\Test1"
    Const dstPath As String = "F:\Test\2020\65412587\Test2"
     
    backupFolder srcPath, dstPath
    
    ' Open Destination Path in File Explorer.
    'ThisWorkbook.FollowHyperlink dstPath

End Sub

' Initialize
Sub backupFolder( _
    ByVal srcPath As String, _
    ByVal dstPath As String, _
    Optional ByVal backupSubFolders As Boolean = True)
    
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    
    With fso
        If .FolderExists(srcPath) Then
            backupFolderCopy fso, srcPath, dstPath
            If backupSubFolders Then
                SkipPath = ""
                backupFolderRecurse fso, srcPath, dstPath
            End If
            MsgBox "Backup updated.", vbInformation, "Success"
        Else
            MsgBox "Source Folder does not exist.", vbCritical, "No Source"
        End If
    End With

End Sub

' Copy Folders
Private Function backupFolderCopy( _
    fso As Object, _
    ByVal srcPath As String, _
    ByVal dstPath As String) _
As String
    
    With fso
        If .FolderExists(dstPath) Then
            Dim fsoFile As Object
            Dim dstFilePath As String
            For Each fsoFile In .GetFolder(srcPath).Files
                dstFilePath = .BuildPath(dstPath, fsoFile.Name)
                ' Or:
                'dstFilePath = Replace(fsoFile.Path, srcPath, dstPath)
                If Not .FileExists(dstFilePath) Then
                    .CopyFile fsoFile.Path, dstFilePath
                End If
            Next fsoFile
            'backupFolderCopy = "" ' redundant: it is "" by default.
        Else
            .CopyFolder srcPath, dstPath
            backupFolderCopy = srcPath
        End If
    End With

End Function

' Copy SubFolders
Private Sub backupFolderRecurse( _
        fso As Object, _
        ByVal srcPath As String, _
        ByVal dstPath As String)
    
    Dim fsoFolder As Object: Set fsoFolder = fso.GetFolder(srcPath)
    
    Dim fsoSubFolder As Object
    Dim srcNew As String
    Dim dstNew As String
    
    For Each fsoSubFolder In fsoFolder.SubFolders
        srcNew = fsoSubFolder.Path
        dstNew = fso.BuildPath(dstPath, fsoSubFolder.Name)
        ' Or:
        'dstNew = Replace(srcNew, srcPath, dstPath)
        If Len(SkipPath) = 0 Or Left(srcNew, Len(SkipPath)) <> SkipPath Then
            SkipPath = backupFolderCopy(fso, srcNew, dstNew)
            backupFolderRecurse fso, srcNew, dstNew
        End If
    Next

End Sub

【讨论】:

    【解决方案2】:

    请尝试下一个代码:

    Sub testCopyFolder()
     Dim FSO As Object, SourceFold As String, DestinationFold As String
     
     SourceFold = "Source folder path"           ' ending in "\"
     DestinationFold = "Destination folder path" ' ending in "\"
     Set FSO = CreateObject("Scripting.FileSystemObject")
     
     If Not FSO.FolderExists(DestinationFold) Then
        FSO.CopyFolder SourceFold, DestinationFold
     End If
    End Sub
    

    您可以以类似的方式继续复制文件。当然,使用FSO.FileExists()...

    【讨论】:

    • 感谢您的帮助-我认为我遇到的主要问题是如何为每个主文件夹中的所有文件/文件夹循环以跳过新文件夹中已经存在的内容。就像主文件夹有 test1.xslx 和子文件夹 test2,而 test2 中有文件要检查 - 依此类推。有没有办法在其中设置一个循环来检查主文件夹中的所有子文件夹和文件?
    • @mphillies2008:当然是这样。但这是一个不同的问题......你至少应该提到这一点。除了存在问题的现有文件夹复制之外,您没有向我们展示您的任何代码。 创建并声明了用于解决您的问题的必要变量...
    • @mphillies2008:不管怎样,在所有文件、文件夹和子文件夹之间进行迭代并没有那么复杂,但是你定义了要复制的地方吗?
    猜你喜欢
    • 2016-08-30
    • 2015-09-07
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2011-05-14
    相关资源
    最近更新 更多