Macro folder zoeken

Status
Niet open voor verdere reacties.

cverkooyen

Gebruiker
Lid geworden
13 sep 2006
Berichten
140
Goede avond,

weet iemand hoe ik met een macro een bepaalde folder zou kunnen zoeken waarvan enkel het begin van de naam van de folder bekend is?

Wanneer de gehele naam bekend is kan ik doen:

Code:
Sub FindFolder()

Dim Folder As Variant

On Error GoTo NoFolder

'Map waarop gecontroleerd moet worden
Folder = "X:\Temp\2009"

CreateObject("Scripting.FileSystemObject").GetFolder (Folder)
MsgBox "Map " & Folder & " bestaat!!!"
Exit Sub
NoFolder:
MsgBox "Map " & Folder & " bestaat niet!!!"

End Sub

Kan iemand mij misschien verder op weg helpen?
 
Hallo cverkooyen,

Het zoeken van een map is nogal ingewikkeld, vooral met eventuele meerdere resultaten en recursief zoeken (in submappen) maar ik heb wel een oplossing voor je.

Probeer onderstaande code eens uit.

Plaats het in een nieuwe module.
De regel "Option compare text" moet ook aanwezig zijn, omdat je anders problemen krijgt met o.a. hoofdletters / kleine letters.

Laat maar weten of het werkt.

Groeten,
Mark.

Code:
Option Explicit
Option Compare Text

Sub Voorbeeld()
Dim strDemap As String

    strDemap = FindDir("2009", "X:\", True)
    
    If Len(strDemap) > 0 Then
    
        MsgBox "het bestand is gevonden : " & strDemap
        
    End If

End Sub

Function FindDir(ByVal strLookFor As String, _
                ByVal strPath As String, _
                Optional ByVal IncludeSubDirs As Boolean) As String
Dim strFolders As String
Dim strFolderList() As String
Dim strMatch As String
Dim strMatching() As String
Dim msgAnswer As VbMsgBoxResult
Dim i As Long

    strFolders = FindFolders(strPath, True)
    strFolderList = Split(Mid(strFolders, 2), vbCr)
    
    For i = LBound(strFolderList) To UBound(strFolderList)
        
        If InStr(strFolderList(i), strLookFor) > 0 Then
            strMatch = strMatch & strFolderList(i) & vbCr
        End If
    
    Next i
    
    On Error GoTo NoFiles
    strMatching = Split(Mid(strMatch, 1, Len(strMatch) - 1), vbCr)
    
    If LBound(strMatching) = UBound(strMatching) Then
        FindDir = strMatching(0)
    Else
        'pick one of the files
        FindDir = SelectFile(strMatching)
    End If
    
    Exit Function
    
NoFiles:
    MsgBox "Geen bestanden gevonden!", vbInformation
End Function

Private Function FindFolders(ByVal strRootDir As String, _
                     Optional ByVal bolRecurse As Boolean) As String
Dim strFound As String
Dim strNextPath As String
Dim strList As String
Dim strSubfolders() As String
Dim i As Long

    strNextPath = Dir(strRootDir, vbDirectory)
    
    Do While strNextPath <> ""
        
        If GetAttr(strRootDir & strNextPath) = vbDirectory And _
            InStr(strNextPath, ".") <> 1 Then
            
            strFound = strFound & vbCr & strRootDir & strNextPath
        
        End If
        
        strNextPath = Dir()
    
    Loop
        
    strList = strFound
    
    If bolRecurse Then
    
        strSubfolders = Split(Mid(strFound, 2), vbCr)
        
        For i = LBound(strSubfolders) To UBound(strSubfolders)
            
            strList = strList & FindFolders(strSubfolders(i) & "\", bolRecurse) & vbCr
        
        Next
    
    End If
    
    FindFolders = strList

End Function

Private Function SelectFile(strList() As String) As String
Dim i As Long
Dim strmsg
Dim vAnswer As Variant
If UBound(strList) > 15 Then
    MsgBox "Er zijn " & UBound(strList) & " Bestanden gevonden." & vbCr & _
            "Probeer uw zoekopdracht te verfijnen. ", vbInformation
Else
    
    For i = LBound(strList) To UBound(strList)
    
        strmsg = strmsg & i + 1 & vbTab & strList(i) & vbCr
    
    Next
    
    vAnswer = InputBox("Geef het nummer op van de juiste map" & vbCr & strmsg)
    If vAnswer <> False And _
        vAnswer > LBound(strList) And _
        vAnswer <= UBound(strList) + 1 Then

        SelectFile = strList(vAnswer - 1)
    
    End If

End If
    
End Function
 
Laatst bewerkt:
Hey Marc,

bedankt voor je reactie. Ik begrijp de code nog niet helemaal maar ik ga ermee aan de slag. Ik heb wel als voordeel dat alles in één map.

bij de functie dindir word er in de code verder toch niks gedaan met IncludeSubDirs of kijk ik ergens over heen?
 
Laatst bewerkt:
Ha cverkooyen,

je hebt gelijk.

in de Functie FindDir
moet je
Code:
strFolders = FindFolders(strPath, True)
vervangen door
Code:
strFolders = FindFolders(strPath, IncludeSubDirs)

anders wordt er altijd in subdirectories gezocht. :)
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan