VBA search for a specific subfolder in many folder

2019-09-19 15:29发布

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 move their files.

P:\Desktop\Folder1\subfolder\SUBFOLDER1\Xfolder

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

2条回答
Ridiculous、
2楼-- · 2019-09-19 15:47

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-09-19 15:51

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

查看更多
登录 后发表回答