VBA: Finding directory by partial name

I'm writing VBA script and I need to be able to use a path for a FileSearch object, where I only know the partial directory name.

For instance:
part of path that I know =  '20015'
directory that I need to find and use =   '20015 - Blah Blah Blah'

Ex:

partialpath = 20015

path = "C:\blah\blahblah\" + [method to find the full directory name from partialpath]

        With Application.FileSearch
            .NewSearch
            .LookIn = path
            .FileType = msoFileTypeAllFiles
            .Execute
LVL 3
CheetoBanditoAsked:
Who is Participating?
 
jake072Connect With a Mentor Commented:
Hmm,

I'm not sure if you have full .Net support, but try the following sample...

Sorry I don't know more about Office & VB Editor...

Jake
    Private Function FindDirectory(ByVal strSearch As String) As String
 
        Dim path As String = "c:\\"
        Dim searchPattern As String = strSearch & "*"
 
        Dim di As IO.DirectoryInfo = New IO.DirectoryInfo(path)
        Dim directories() As IO.DirectoryInfo = di.GetDirectories(searchPattern, IO.SearchOption.TopDirectoryOnly)
 
        If directories.Length > 1 Then Return directories(0).Name
 
        Return ""
 
    End Function

Open in new window

0
 
jake072Commented:
I'm not well versed in VBA script, can this be written in .Net?

Jake
0
 
CheetoBanditoAuthor Commented:
Not sure.  I'm currently working with Office 2003 and the Visual Basic editor that ships with that.  I assumed that was VBA
0
 
CheetoBanditoAuthor Commented:
I established a workaround.  I think your code would work, but I can't run that with the version of my client's software.
0
All Courses

From novice to tech pro — start learning today.