-1

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
mitsuki
  • 1
  • 2
  • 7
  • you have a ghost doublequote there: `If Dir(destinationFolder ", 16)` fix this first – Thomas G May 25 '16 at 12:17
  • so you turned your question from "_can you help me to spot the problem in this code_" which was fine, into "_could you code this for me_" which is not fine. – Thomas G May 25 '16 at 12:21
  • ok you changed it again. There was no VBA code anymore when I posted my comment – Thomas G May 25 '16 at 12:25

2 Answers2

1

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.

tk78
  • 937
  • 7
  • 14
  • can you please explaine more the strStartFolder ? i just put there the path – mitsuki May 27 '16 at 09:15
  • @mitsuki yes, just put the path of the folder you want to scan. StrMoveFolder is the name of the folder you are looking for. StrTargetFolder ise destination path for the files moved. – tk78 May 28 '16 at 14:13
0

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

Luboš Suk
  • 1,526
  • 14
  • 38