Link to home
Start Free TrialLog in
Avatar of Patrick Matthews
Patrick MatthewsFlag for United States of America

asked on

Function needed to recursively search a directory for a named subdirectory

Normally I would just do this myself, but I am a bit pressed for time :)

I need a function that will locate a named subdirectory within a parent directory, and have as its
return value the path string to the subdirectory.

Here is an example.

Assume the following directories exist:

c:\Start here\Location 1\Team 1\Alice
c:\Start here\Location 1\Team 1\Ben
c:\Start here\Location 1\Team 1\Charlene
c:\Start here\Location 1\Team 2\David
c:\Start here\Location 1\Team 2\Ernie
c:\Start here\Location 1\Team 2\Frances
c:\Start here\Location 2\Team 3\George
c:\Start here\Location 2\Team 3\Harry
c:\Start here\Location 2\Team 3\Ivana
c:\Start here\Location 2\Team 4\John
c:\Start here\Location 2\Team 4\Karla
c:\Start here\Location 2\Team 4\Lance
c:\Start here\Location 3\Team 5\Mary
c:\Start here\Location 3\Team 5\Neville
c:\Start here\Location 3\Team 5\Opal
c:\Start here\Location 3\Team 6\Quentin
c:\Start here\Location 3\Team 6\Rachel
c:\Start here\Location 3\Team 6\Stephen

All of the people's folders exist somewhere in c:\Start here, or in one of the folder's "descendants".

I need a function to which I could pass arguments like "c:\Start here" and "Quentin", and then the function
would search recursively through c:\Start here and its descendants until it finds
c:\Start here\Location 3\Team 6\Quentin, and would return the string "c:\Start here\Location 3\Team 6\Quentin".

Thanks,

Patrick

Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

ASKER

BTW, I want the function to stop on the first positive match; if there are >1 subfolder meeting the search
criteria, shame on me.

Patrick
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!  works just as I needed it to.

Regards,

Patrick
angelIII,

The only change of any substance was to change:

        if subfolder.name = searchname then

to:

        If StrComp(subfolder.Name, SearchName, vbTextCompare) = 0 Then

thus making the comparison not case-sensitive (assuming the default text mode is binary; if I used Option Compare Text
then it's moot).

Regards,

Patrick