使用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.
我的代码指向一个目录包含60个左右的子文件夹。这些子文件夹中有多个.PDF / .XLS等文件。如果文件未嵌入子文件夹中,则以下代码可以正常工作,但是我需要做的是能够遍历子文件夹并拉动文件本身进行移动。另外,有没有办法最终通过通配符名称提取文件?预先感谢您的帮助。
My code points at a directory that contains 60 or so subfolders. 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. 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
这篇关于使用vba从多个子文件夹复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!