VBA search for a specific subfolder in many folders and move all the files in it

前端 未结 2 1849
天涯浪人
天涯浪人 2021-01-29 06:16

can you help me?

i want a macro vba that search for a SPECIFIC subfolder for example (Xfolder) between all the folders and subfolders that exist and mov

2条回答
  •  不知归路
    2021-01-29 06:43

    Here's a solution:

    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 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.

提交回复
热议问题