Results 1 to 6 of 6

Thread: To search a folder in a path

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Posts
    68

    To search a folder in a path

    i store different paths dynamically at runtime in a variable...now i should check whether a paticular folder called "\BIN" is there in my folder or not...if the folder is not there in the path then i should throw an error...how could this be done?

  2. #2
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: To search a folder in a path

    Code:
    Dim fso As Object
    Set fso = CreateObject("scripting.filesystemobject")
    If fso.FolderExists("C:\attachments") = False Then
        MsgBox "Booooo!"
    Else
        MsgBox "It's here"
    End If

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: To search a folder in a path

    No need to weigh down your project with an fso dependency:
    Code:
    Private Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
    Private Declare Function PathIsDirectory Lib "shlwapi" Alias "PathIsDirectoryA" (ByVal pszPath As String) As Long
    
    Public Function FileExists(pstrFile As String) As Boolean
        FileExists = (PathFileExists(pstrFile) = 1)
        If FileExists Then FileExists = (PathIsDirectory(pstrFile) = 0)
    End Function
    
    Public Function FolderExists(pstrFolder As String) As Boolean
        FolderExists = (PathFileExists(pstrFolder) = 1)
        If FolderExists Then FolderExists = (PathIsDirectory(pstrFolder) <> 0)
    End Function

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: To search a folder in a path

    No real need for APIs either
    Code:
    Public Function FolderExists(pstrFolder As String) As Boolean
    If Dir$(pstrFolder, vbDirectory) <> vbNullString Then
       'it does exist
       FolderExists = True
    Else
       'it does not exist
       FolderExists = False
    End If
    End Function

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: To search a folder in a path

    Quote Originally Posted by Hack
    No real need for APIs either
    Code:
    Public Function FolderExists(pstrFolder As String) As Boolean
    If Dir$(pstrFolder, vbDirectory) <> vbNullString Then
       'it does exist
       FolderExists = True
    Else
       'it does not exist
       FolderExists = False
    End If
    End Function
    The advantage of the API calls is that they don't co-opt the Dir stack. I got the impression that the OP may be using a Dir loop for other purposes.

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: To search a folder in a path

    Quote Originally Posted by Ellis Dee
    The advantage of the API calls is that they don't co-opt the Dir stack.
    Totally agree, using Dir command in Functions is just not a good idea IMO and just asking for trouble especially if you re-use functions in other projects.

    Heres another non-API way,

    Code:
    Public Function bDirExists(sDir As String) As Boolean
    ' Tests for directory existence,
    ' Returns False if not found, True if found.
        On Error Resume Next
        bDirExists = ((GetAttr(sDir) And vbDirectory) <> 0)
    End Function

Posting Permissions

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



Click Here to Expand Forum to Full Width