【问题标题】:Copying files from one folder to another using vba使用 vba 将文件从一个文件夹复制到另一个文件夹
【发布时间】:2014-12-02 05:30:36
【问题描述】:

我知道有一些关于这个主题的类似帖子。但是,我的代码与我在这里看到的所有代码都不同(在谈论这个主题时)。

我收到的错误是找不到文件。但这有点不可能,因为我在 fso.CopyFile 中用作 SOURCE 的同一文件夹中搜索文件。

所以我必须修复这个错误,如果可能的话,我想将文件复制到另一个文件夹并更改名称。例如,如果我有文件“Excel.xls”,我想用名称“Excel_old.xls”进行复制,是否可以使用下面的代码,还是太难不值得?

这是代码:

Sub CopyFiles()
'Macro to copy all files modified yesterday

Dim n As String, msg As String, d As Date
Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set fils = fso.GetFolder("C:\Users\Desktop\Files\").Files

'Verify all files in the folder, check the modification date and then copy 
'to another folder (named Old)
For Each fil In fils
    n = fil.Name
    d = fil.DateLastModified
    If d >= Date - 1 Then
        file = n
        'The following line is where the error occurs
        fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file"

    End If
Next fil

End Sub

【问题讨论】:

    标签: excel vba file directory


    【解决方案1】:

    这是因为fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file" 不是文件...从外观上看,它只是一个虚拟文件的字符串。

    如果该行是

    fso.CopyFile fil.Path, "C:\Users\Desktop\Files\Old\" & fil.name... 这可能行得通。

    更新添加:

    我刚刚尝试了以下使用(在下面替换计算机用户名)并成功地将所有内容移动到一个新文件夹中:

    Sub test()
        Dim fso As FileSystemObject
        Dim fsoFiles As Files
        Dim fil As File
    
        Set fso = New FileSystemObject
        Set fils = fso.GetFolder("C:\Users\<MY USERNAME>\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
    

    这里唯一的区别是我使用 fil.ParentFolder 来获取我的桌面,然后将它扔到我在桌面上创建的名为“test”的新文件夹中(在运行脚本之前)。

    【讨论】:

    • 没错。但是有一个错字,在fso.CopyFile之后,根据OP的代码,它应该是filefil.name
    • 是的..应该是fil.path而不是fil.name。我很抱歉。我想我对 fso 有点生疏了。
    • 使用 fso.CopyFile fil.Path, "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file" 并得到相同的错误,类型不匹配。我也尝试了fso.CopyFile fil.Path, "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\",但仍然出现错误 13
    • 不应该是:"C:\Users\Desktop\Files\" & 文件,"C:\Users\Desktop\Files\Old\" & 文件
    • 试过了,又是同样的错误,我正在搜索,但我真的很挣扎
    【解决方案2】:

    用于移动文件夹中的所有文件:

    Sub MoveFiles()
    
    Dim MyFile As String
    
    MyFile = Dir("C:\AAAA\*.*")
    
    Do Until MyFile = ""
    
    Name "C:\AAAA\" & MyFile As "C:\AAA\" & MyFile
    
    MyFile = Dir
    
    Loop
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      要递归复制所有文件和子文件夹,请使用以下代码:

      Public Sub CopyDirectory(ByVal source As String, ByVal destination As String)
          Dim fso, file, folder As Object
          Set fso = CreateObject("Scripting.FileSystemObject")
          'Delete existing folder
          If fso.FolderExists(destination) Then fso.DeleteFolder destination, True
          fso.CreateFolder destination
          For Each file in fso.GetFolder(source).files
              fso.CopyFile file.Path, destination & "\" & file.Name
          Next file
          For Each folder in fso.GetFolder(source).SubFolders
              CopyDirectory folder.Path, destination & "\" & folder.Name
          Next folder
      End Sub
      

      如下使用:

      CopyFile "C:\Path To Source", "C:\Path to destination"
      

      请注意,路径不应包含尾随目录分隔符 (\)。

      【讨论】:

      • 您可能会如何设置它,以便在复制之前检查目标文件夹中是否存在文件(最新版本?)?
      猜你喜欢
      • 1970-01-01
      • 2021-03-16
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 2023-01-14
      • 2017-07-23
      相关资源
      最近更新 更多