【问题标题】:Trying to move multiple files from one folder to another but getting error at filecopy row尝试将多个文件从一个文件夹移动到另一个文件夹,但在 filecopy 行出现错误
【发布时间】:2018-09-11 01:33:33
【问题描述】:

尝试将多个文件从一个文件夹移动到另一个文件夹,但在文件复制行出现错误。我尝试删除直接传递给路径的字符串,如下所示,但仍然抛出错误 - “找不到文件”

Sub MoveFiles_3()
    Dim fso As Object
    Dim d As String, ext, x
    Dim srcPath As String, destPath As String, srcFile As String

    srcPath = "C:\Users\rohit.keskar\Desktop\Test Macro"
    destPath = "C:\Users\rohit.keskar\Desktop\Archive Test"
    Set fso = CreateObject("Scripting.FileSystemObject")
    ext = Array("*.xlsx")
    MsgBox Dir(srcPath)

    For Each x In ext
        d = Dir(srcPath & x)

        Do While d <> ""
            srcFile = srcPath & d
            fso.CopyFile "C:\Users\rohit.keskar\Desktop\Test Macro" & d, "C:\Users\rohit.keskar\Desktop\Archive Test" & d
            Kill "C:\Users\rohit.keskar\Desktop\Test Macro" & d
            d = Dir
        Loop
    Next
    MsgBox "done"
End Sub

【问题讨论】:

  • 每条路径的末尾都需要一个反斜杠。
  • 谢谢,我一贴在这里就意识到了。

标签: vba excel


【解决方案1】:

请尝试以下。

    Sub MoveFiles_3()

Dim fso As Object, f As Object
Dim d As String, ext, x
Dim srcPath As String, destPath As String, srcFile As String

srcPath = "srcpath"
destPath = "destPath"

Set fso = CreateObject("Scripting.FileSystemObject")

ext = "xlsx"

'MsgBox Dir(srcPath)

For Each f In fso.getfolder(srcPath).Files
If Right(f, 4) = ext Then
    fso.CopyFile f.Path, destPath & "\" & f.Name
End If
Next
MsgBox "done"
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多