Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Finding a partial folder name (VBA Word 2000)

    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

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding a partial folder name (VBA Word 2000)

    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!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding a partial folder name (VBA Word 2000)

    Beautiful!! Just what I was looking for!

    Thanks!!
    Troy

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Finding a partial folder name (VBA Word 2000)

    Or just adapt the original:
    <pre>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
    </pre>

    FWIW.
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding a partial folder name (VBA Word 2000)

    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!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •