VBA在许多文件夹中搜索特定的子文件夹并移动其中的所有文件

时间:2016-05-25 12:05:26

标签: excel vba excel-vba

你可以帮帮我吗?

我想要一个宏vba,在所有存在的文件夹和子文件夹之间搜索 SPECIFIC 子文件夹(Xfolder)并移动他们的文件。

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

我正在使用VBA脚本运行时对象

  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 个答案:

答案 0 :(得分:1)

这是一个解决方案:

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文件的文件夹。

答案 1 :(得分:0)

找到一些文件夹使用类似的东西

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

然后你可以对该文件夹及其内容做任何事情。因此,我很少使用谷歌(一个可能是两个单词),你可以实现你的wana