You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MS Access中Fso.CopyFolder执行卡顿并报“路径未找到”的问题咨询

解决MS Access中FSO复制文件夹中断的问题

原FSO的CopyFolder方法会递归复制所有内容,包括系统隐藏文件(如thumbs.db),一旦遇到无法复制的文件或临时异常,就会直接中断整个复制流程。要实现排除特定文件的复制,需要手动递归遍历文件夹并过滤目标文件,具体方案如下:

自定义递归复制函数(带文件过滤)

通过手动遍历每个文件和子文件夹,跳过指定的排除文件,同时加入错误捕获避免单个文件失败导致整体中断:

Sub CopyFolderWithExclusions(sourcePath As String, destPath As String, Optional excludedFiles As Variant)
    Dim fso As Object
    Dim sourceFolder As Object
    Dim destFolder As Object
    Dim subFolder As Object
    Dim file As Object
    Dim skipFile As Boolean
    Dim exclude As Variant
    
    ' 初始化文件系统对象
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' 默认排除thumbs.db,可自定义扩展排除列表
    If IsMissing(excludedFiles) Then
        excludedFiles = Array("thumbs.db")
    End If
    
    ' 创建目标文件夹(不存在则新建)
    If Not fso.FolderExists(destPath) Then
        Set destFolder = fso.CreateFolder(destPath)
    Else
        Set destFolder = fso.GetFolder(destPath)
    End If
    
    Set sourceFolder = fso.GetFolder(sourcePath)
    
    ' 复制当前文件夹下的文件(跳过排除列表中的文件)
    For Each file In sourceFolder.Files
        skipFile = False
        ' 不区分大小写检查是否在排除列表
        For Each exclude In excludedFiles
            If LCase(file.Name) = LCase(exclude) Then
                skipFile = True
                Exit For
            End If
        Next exclude
        
        If Not skipFile Then
            ' 捕获单个文件复制错误,不中断整体流程
            On Error Resume Next
            fso.CopyFile file.Path, destFolder.Path & "\", True ' True表示覆盖已存在文件
            If Err.Number <> 0 Then
                ' 可选:记录错误信息到调试窗口
                Debug.Print "复制失败: " & file.Path & " | 错误描述: " & Err.Description
                Err.Clear
            End If
            On Error GoTo 0
        End If
    Next file
    
    ' 递归处理子文件夹
    For Each subFolder In sourceFolder.SubFolders
        Call CopyFolderWithExclusions(subFolder.Path, destFolder.Path & "\" & subFolder.Name, excludedFiles)
    Next subFolder
    
    ' 清理对象
    Set file = Nothing
    Set subFolder = Nothing
    Set sourceFolder = Nothing
    Set destFolder = Nothing
    Set fso = Nothing
End Sub

在按钮事件中调用函数

将原有的FSO复制代码替换为自定义函数的调用,可直接在按钮点击事件中使用:

Private Sub cmdCopy_Click()
    ' 基础用法:仅排除thumbs.db
    CopyFolderWithExclusions "S:\oldfolder", "S:\newfolder"
    
    ' 扩展用法:排除多个文件,比如thumbs.db和desktop.ini
    ' CopyFolderWithExclusions "S:\oldfolder", "S:\newfolder", Array("thumbs.db", "desktop.ini")
End Sub

额外说明

  • 该方法会逐个处理文件,即使单个文件复制失败(如临时锁定、权限问题),也不会终止整个复制任务
  • 可根据需求扩展排除列表,添加任何需要跳过的文件名
  • 相比原生CopyFolder,更灵活可控,适合处理包含大量子文件夹的网络路径复制场景

内容的提问来源于stack exchange,提问作者jco23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 20:02:07