Retrieving name of subfolder

pvr928

Well-known Member
Joined
Oct 21, 2002
Messages
790
Hi

I am would like to retrieve the name of the subfolder containing the file I am searching for. I am using this code from excel - VBA macro that search for file in multiple subfolders - Stack Overflow

I do otherwise have code that provides the name of the subfolder, but this method using Collections, is much faster. The downside is - I am not that familiar with Collections !

Any help greatly appreciated.

Cheers

pvr928

Code:
Option Explicit


Sub GetFiles(StartFolder As String, Pattern As String, _
             DoSubfolders As Boolean, ByRef colFiles As Collection)
             
    'http://stackoverflow.com/questions/20687810/vba-macro-that-search-for-file-in-multiple-subfolders


    Dim f As String, sf As String, subF As New Collection, s
    
    If Right(StartFolder, 1) <> "\" Then StartFolder = StartFolder & "\"


    f = Dir(StartFolder & Pattern)
    Do While Len(f) > 0
        colFiles.Add StartFolder & f
        f = Dir()
    Loop


    sf = Dir(StartFolder, vbDirectory)
    Do While Len(sf) > 0
        If sf <> "." And sf <> ".." Then
            If (GetAttr(StartFolder & sf) And vbDirectory) <> 0 Then
                    subF.Add StartFolder & sf
            End If
        End If
        sf = Dir()
    Loop


    For Each s In subF
        GetFiles CStr(s), Pattern, True, colFiles
        Debug.Print CStr(s)
    Next s


End Sub


Sub Find_File()


Dim colFiles As New Collection


GetFiles "E:\TEST\", "FileToFind" & ".pdf", True, colFiles
If colFiles.Count > 0 Then
    
    Debug.Print "Found it!"
    
Else


    Debug.Print "Didn't find it!"


End If


End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi Pvr,
the answer is in your code...

Code:
Sub Find_File()


Dim colFiles As New Collection


GetFiles "E:\TEST\", "FileToFind" & ".pdf", True, colFiles
If colFiles.Count > 0 Then
    For Each s In colFiles
        Debug.Print CStr(s)
    Next s
    Debug.Print "Found it!"
Else
    Debug.Print "Didn't find it!"
End If


End Sub
This code will show you all found files. You could get the directory: vba - Find the directory part (minus the filename) of a full path in access 97 - Stack Overflow
Cheers,
Koen
 
Upvote 0
Hi Koen

Thanks so much ! So simple!

For some reason I was concentrating on the main routine - I didn't think the answer lay in the smaller routine.

Thanks once again.

Cheers

pvr928
 
Upvote 0

Forum statistics

Threads
1,214,846
Messages
6,121,905
Members
449,054
Latest member
luca142

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top