VBA: Finding directory by partial name

Posted on 2009-02-23
Last Modified: 2012-05-06
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'


partialpath = 20015

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

        With Application.FileSearch
            .LookIn = path
            .FileType = msoFileTypeAllFiles
Question by:CheetoBandito
    LVL 14

    Expert Comment

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

    LVL 3

    Author Comment

    Not sure.  I'm currently working with Office 2003 and the Visual Basic editor that ships with that.  I assumed that was VBA
    LVL 14

    Accepted Solution


    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...

        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

    LVL 3

    Author Closing Comment

    I established a workaround.  I think your code would work, but I can't run that with the version of my client's software.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This article will show you how to use shortcut menus in the Access run-time environment.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now