Copying files from one folder to another using vba

后端 未结 3 533
一个人的身影
一个人的身影 2021-01-26 18:17

There are some similar posts about this topic, I know. However, I have a code which is different than all codes I have seen here (when talking about this subject).

The e

3条回答
  •  情书的邮戳
    2021-01-26 19:17

    This is because fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file" is not a file... It's just a string to a dummy file from the looks of it.

    If instead the line was

    fso.CopyFile fil.Path, "C:\Users\Desktop\Files\Old\" & fil.name... that might work.

    Updated to add:

    I just attempted the following using (subbing computer username for below) and had success moving everything into a new folder:

    Sub test()
        Dim fso As FileSystemObject
        Dim fsoFiles As Files
        Dim fil As File
    
        Set fso = New FileSystemObject
        Set fils = fso.GetFolder("C:\Users\\Desktop\").Files
    
        For Each fil In fils
            n = fil.Name
            d = fil.DateLastModified
            fso.CopyFile fil.Path, fil.ParentFolder & "\test\" & fil.Name
    
        Next fil
    End Sub
    

    The only difference here is that I used fil.ParentFolder to get my Desktop and then tossed it into a new folder I created on my desktop (prior to running the script) named "test".

提交回复
热议问题