VBA在许多文件夹中搜索特定的子文件夹并移动其中的所有文件 [英] VBA search for a specific subfolder in many folders and move all the files in it
本文介绍了VBA在许多文件夹中搜索特定的子文件夹并移动其中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你能帮我吗?
我想要一个宏vba,该宏可以在所有存在的文件夹和子文件夹之间搜索 SPECIFIC 子文件夹(例如Xfolder)并移动它们的文件.
i want a macro vba that search for a SPECIFIC subfolder for example (Xfolder) between all the folders and subfolders that exist and move their files.
P:\Desktop\Folder1\subfolder\SUBFOLDER1\Xfolder
我正在使用VBA脚本运行时对象
I'm using the VBA Scripting Runtime objects
Set oSourceFolder = fso.GetFolder(source)
If Dir(destinationFolder, 16) = "" Then MkDir (destinationFolder)
For Each oFile In oFolder.Files
If Dir(destinationFolder,16) = "" Then
fso.MoveFile oFile.Path, destinationFolder
End If
Next oFile
fso.DeleteFolder oFolder.Path
Next oFolder
推荐答案
这是一个解决方案:
Dim fsoFileSystem As New FileSystemObject
Dim foFolder As Folder, foSubFolder As Folder
Dim fFile As File
Dim strStartFolder As String, strMoveFolder As String, strTargetFolder As String
strStartFolder = "\\A\B\C"
strMoveFolder = "SearchFolder"
strTargetFolder = "\\B\D\E"
Set foFolder = fsoFileSystem.GetFolder(strStartFolder)
For Each foSubFolder In foFolder.SubFolders
If foSubFolder.Name = strMoveFolder Then
For Each fFile In foSubFolder.Files
fsoFileSystem.MoveFile fFile, strTargetFolder & "\"
Next
End If
Next
strStartFolder是要显示子文件夹的Screen的文件夹. strMoveFolder是要查找的文件夹的名称. strTargetFolder是所有strMoveFolder文件应移动到的文件夹.
strStartFolder is the folder to Screen for subfolders. strMoveFolder is the name of the Folder to look for. strTargetFolder is the Folder to where all the strMoveFolder's files shall be moved.
这篇关于VBA在许多文件夹中搜索特定的子文件夹并移动其中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文