【问题标题】:Move all file and folder to another folder in Access VBA将所有文件和文件夹移动到 Access VBA 中的另一个文件夹
【发布时间】:2014-12-17 20:10:52
【问题描述】:

我想备份所有文件和文件夹。

例如:我有 2 个文件和一个位于 D:\Temp 文件夹中的文件夹。

我在同一文件夹中还有一个名为“Backup”的文件夹,即 D:\Temp\

所以我想将除备份文件夹之外的所有文件和文件夹移动到 Access VBA 中的备份文件夹。这可能吗?

这是我迄今为止从 Ron 的代码中尝试过的。

我尝试从一个文件夹移动到临时文件夹,然后从该文件夹移动到我的原始文件夹,如下所示。但我遇到了错误。

savepath = "d:\test\"
savepath2 = "d:\temp\"
savepath1 = "d:\test\Archieve\"

Dim FSO As Object
Dim FromPath As String
Dim ToPath As String

FromPath = savepath  '<< Change
ToPath = savepath2 '<< Change

If Right(FromPath, 1) = "\" Then
    FromPath = Left(FromPath, Len(FromPath) - 1)
End If

If Right(ToPath, 1) = "\" Then
    ToPath = Left(ToPath, Len(ToPath) - 1)
End If

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FolderExists(FromPath) = False Then
    MsgBox FromPath & " doesn't exist"
    Exit Sub
End If

FSO.moveFolder Source:=FromPath, Destination:=ToPath


If Not DirExists(savepath) Then
MkDir (savepath)
End If

If Not DirExists(savepath1) Then
MkDir (savepath1)
End If

FromPath = savepath2  '<< Change
ToPath = savepath1 '<< Change

If Right(FromPath, 1) = "\" Then
    FromPath = Left(FromPath, Len(FromPath) - 1)
End If

If Right(ToPath, 1) = "\" Then
    ToPath = Left(ToPath, Len(ToPath) - 1)
End If

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FolderExists(FromPath) = False Then
    MsgBox FromPath & " doesn't exist"
    Exit Sub
End If


FSO.moveFolder Source:=FromPath, Destination:=ToPath

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    两个问题:(1)你没有识别出哪一行代码有错误,这使得其他人花费额外的时间查看你所有的代码;

    (2) 你的语法是错误的 - 改变

    If Not DirExists(savepath) Then
    

    成为:

    If Not FSO.FolderExists(savepath) Then
    

    【讨论】:

      【解决方案2】:

      Windows 10 中也不允许移动。您必须复制文件和/或文件夹,然后将其删除。如果删除文件夹,您需要确保该文件夹为空,否则会出现错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-24
        • 2019-03-21
        • 2019-10-27
        • 1970-01-01
        • 2022-08-10
        相关资源
        最近更新 更多