尝试将多个文件从一个文件夹移动到另一个文件夹,但在文件复制行出现错误

时间:2018-09-11 01:33:34

标签: excel vba excel-vba

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

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

1 个答案:

答案 0 :(得分:0)

请尝试以下操作。

    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