简体   繁体   English

使用vba从多个子文件夹复制文件

[英]copying files from multiple subfolders using vba

I've seen some documentation on this but so far, nothing that I've been able to replicate for my specific project.我已经看到了一些关于此的文档,但到目前为止,我无法为我的特定项目复制任何内容。

My code points at a directory that contains 60 or so subfolders.我的代码指向一个包含 60 个左右子文件夹的目录。 Within these subfolders are multiple files .PDF/.XLS etc. The following code works fine if the files are not embedded in the subfolders but what I need to do is be able to loop through the subfolders and pull the files themselves to move.在这些子文件夹中有多个文件 .PDF/.XLS 等。如果文件没有嵌入子文件夹中,下面的代码工作正常,但我需要做的是能够遍历子文件夹并拉动文件本身移动。 Also, is there a way to eventually pull files by wildcard name?另外,有没有办法最终通过通配符名称提取文件? Thanks in advance for any help.在此先感谢您的帮助。

  Dim FSO As Object
  Dim FromPath As String
  Dim ToPath As String
  Dim Fdate As Date
  Dim FileInFromFolder As Object

  FromPath = "H:\testfrom\"
  ToPath = "H:\testto\"

  Set FSO = CreateObject("scripting.filesystemobject")
  For Each FileInFromFolder In FSO.getfolder(FromPath).Files
  Fdate = Int(FileInFromFolder.DateLastModified)
      If Fdate >= Date - 1 Then

        FileInFromFolder.Copy ToPath

    End If
Next FileInFromFolder
End Sub

You can also use recursion.您也可以使用递归。 Your folder can have subfolders having subfolders having ...您的文件夹可以有子文件夹,子文件夹有...

Public Sub PerformCopy()
    CopyFiles "H:\testfrom\", "H:\testto\"
End Sub


Public Sub CopyFiles(ByVal strPath As String, ByVal strTarget As String)
    Set FSO = CreateObject("scripting.filesystemobject")
    'First loop through files
    For Each FileInFromFolder In FSO.getfolder(strPath).Files
        Fdate = Int(FileInFromFolder.DateLastModified)
        If Fdate >= Date - 1 Then
            FileInFromFolder.Copy strTarget
        End If
    Next FileInFromFolder 


    'Next loop throug folders
    For Each FolderInFromFolder In FSO.getfolder(strPath).SubFolders
        CopyFiles FolderInFromFolder.Path, strTarget
    Next FolderInFromFolder
End Sub

I managed to get this code to work.我设法让这段代码工作。 It copies all folders / files and sub folders and their files to the new destination (strTarget).它将所有文件夹/文件和子文件夹及其文件复制到新目标 (strTarget)。

I have not added checks and balances like 1) if the files and folders exist already.如果文件和文件夹已经存在,我还没有添加像 1) 这样的检查和平衡。 2) if the source files are open etc. So those additions could be useful. 2) 如果源文件是打开的等等。所以这些添加可能很有用。

I got this code from Barry's post but needed to change it to make it work for me, so thought i'd share it again anyway.我从巴里的帖子中得到了这段代码,但需要对其进行更改以使其对我有用,所以我想无论如何我都会再次分享它。

Hope this is useful though.希望这很有用。 . . . .

strPath is the source path and strTarget is the destination path. strPath 是源路径, strTarget 是目标路径。 both paths should end in '\\'两条路径都应以“\\”结尾

Note: one needs to add "Microsoft Scripting Runtime" under "Tools / References" for FSO to work.注意:需要在“工具/参考”下添加“Microsoft Scripting Runtime”才能使 FSO 工作。

==================== call ================================
MkDir "DestinationPath"

CopyFiles "SourcePath" & "\", "DestinationPath" & "\"

==================== Copy sub ===========================

Public Sub CopyFiles(ByVal strPath As String, ByVal strTarget As String)
Dim FSO As Object
Dim FileInFromFolder As Object
Dim FolderInFromFolder As Object
Dim Fdate As Long
Dim intSubFolderStartPos As Long
Dim strFolderName As String

Set FSO = CreateObject("scripting.filesystemobject")
'First loop through files
    For Each FileInFromFolder In FSO.GetFolder(strPath).Files
        Fdate = Int(FileInFromFolder.DateLastModified)
        'If Fdate >= Date - 1 Then
            FileInFromFolder.Copy strTarget
        'end if
    Next

    'Next loop throug folders
    For Each FolderInFromFolder In FSO.GetFolder(strPath).SubFolders
        'intSubFolderStartPos = InStr(1, FolderInFromFolder.Path, strPath)
        'If intSubFolderStartPos = 1 Then

        strFolderName = Right(FolderInFromFolder.Path, Len(FolderInFromFolder.Path) - Len(strPath))
        MkDir strTarget & "\" & strFolderName

        CopyFiles FolderInFromFolder.Path & "\", strTarget & "\" & strFolderName & "\"

    Next 'Folder

End Sub

I found the solution here:我在这里找到了解决方案:

 Private Sub Command3_Click()

Dim objFSO As Object 'FileSystemObject
Dim objFile As Object 'File
Dim objFolder As Object 'Folder
Const strFolder As String = "H:\testfrom2\"
Const strNewFolder As String = "H:\testto\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFolder In objFSO.GetFolder(strFolder & "\").SubFolders
    'If Right(objFolder.Name, 2) = "tb" Then
        For Each objFile In objFolder.Files
            'If InStr(1, objFile.Type, "Excel", vbTextCompare) Then
                On Error Resume Next
    Kill strNewFolder & "\" & objFile.Name
 Err.Clear: On Error GoTo 0

                Name objFile.Path As strNewFolder & "\" & objFile.Name
            'End If
        Next objFile
    'End If
Next objFolder


End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 VBA 从子文件夹复制文件 - Copying Files from Subfolders Using VBA VBA遍历子文件夹以打开多个文件 - VBA loop through subfolders to open multiple files VBA - 从多个 Excel 文件复制和粘贴到单个 Excel 文件 - VBA - Copying and Pasting from Multiple Excel files to Single Excel File 使用vba将文件从一个文件夹复制到另一个文件夹 - Copying files from one folder to another using vba 使用VBA将数据从文件夹中的不同文件复制到主表 - using VBA copying data from different files in a folder to master sheet Excel VBA将一个文件夹中的多个文件中的多个工作表复制到一个文件中的多个工作表中 - Excel VBA copying multiple sheets from multiple files in a folder into multiple sheets in one file Excel VBA在文件夹和子文件夹中搜索并返回多个文件 - Excel VBA Search in folder and subfolders and returns multiple files 使用 python 将数据从多个 excel 文件复制到指定列 - Copying data from multiple excel files to specified columns using python 在子文件夹中搜索.msg文件,然后使用VBA Excel将其与Outlook一起邮寄 - Searching for .msg files in subfolders and mailing them with Outlook using VBA Excel 从不同的子文件夹访问VBA导入特定的Excel文件 - Access VBA Import Specific Excel Files from different subfolders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM