• Finding a partial folder name (VBA Word 2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Finding a partial folder name (VBA Word 2000)

    Author
    Topic
    #378190

    If seen the function:

    Public Function fFolderExists(strFullName As Variant) As Boolean
    If Len(strFullName) 0 Then
    fFolderExists = Len(Dir(strFullName, vbDirectory))
    End If
    End Function

    But this tries to find the full path.

    What I want is to determine if there exists a directory at a certain location that starts with a specific two characters.

    For example, find if a directory exists at “C:Test” that starts with “ab”.

    Thus it would return true if folder “C:Testabcdefg” or “C:Testab123” exists.

    Thanks for the help!!
    Troy

    Viewing 0 reply threads
    Author
    Replies
    • #625211

      You’ve got to enumerate the folder list.

      Nothing in VBA will do this unless you roll your own. Fortunately,
      the FileSystem object of the scripting runtime library has the
      subfolders collection — just what you need!

      Try this:
      Function fSubFolderExists(sRootPath As String, sSub As String) As Boolean
      Dim oFso As Object
      Dim oTopFolder As Object
      Dim oSubs As Object
      Dim oFolder As Object

      Set oFso = CreateObject(“Scripting.FileSystemObject”)
      Set oTopFolder = oFso.GetFolder(sRootPath)
      Set oSubs = oTopFolder.SubFolders
      For Each oFolder In oSubs
      If oFolder.Name Like sSub & “*” Then
      fSubFolderExists = True
      set oFso = Nothing
      Exit Function
      End If
      Next
      End Function

      BTW, I highly recommend the FSO model for all your
      file, folder and drive I/O. Here!

      • #625248

        Beautiful!! Just what I was looking for!

        Thanks!!
        Troy

      • #625332

        Or just adapt the original:

        Public Function fFolderExists(strRootDir As Variant, strPartName As Variant) As Boolean
        If Right$(strRootDir, 1)  "" Then strRootDir = strRootDir & ""
        If Len(strPartName)  0 Then
        fFolderExists = Len(Dir(strRootDir & strPartName & "*", vbDirectory))
        End If
        End Function
        

        FWIW.

        • #625409

          It would help to read the help would it?

          “Dir supports the use of multiple-character (*) and single-character (?) wildcards to specify multiple files.”

          Good show, Rory!

    Viewing 0 reply threads
    Reply To: Finding a partial folder name (VBA Word 2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: