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

前端 未结 2 1810
天涯浪人
天涯浪人 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.

    0 讨论(0)
  • 2021-01-29 06:43

    To found some folder use something like this

    Sub findFolder()
    
        Dim searchFolderName As String
        searchFolderName = "somePath"
    
        Dim FileSystem As Object
    
        Set FileSystem = CreateObject("Scripting.FileSystemObject")
    
        doFolder FileSystem.getFolder(searchFolderName)
    
    
    
    End Sub
    
    Sub doFolder(Folder)
        Dim subFolder
        On Error Resume Next
        For Each subFolder In Folder.subfolders
            If Split(subFolder, "\")(UBound(Split(subFolder, "\"))) = "testFolder" Then
                MsgBox "gotcha"
            End
            End If
    
            doFolder subFolder
        Next subFolder
    
    End Sub
    

    And then you can do whatever with that folder and its content. So with i little use of google (one maybe two words) you can achieve what you wana

    0 讨论(0)
提交回复
热议问题