Results 1 to 3 of 3

Thread: FolderExists Issue

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    803

    FolderExists Issue

    Hi All

    I recently started to get some errors in my Folderexists function. If the Folder name was not the same case it would cause it to return False.

    Code:
    Private Const MAX_PATH As Long = 260
    Private Const INVALID_HANDLE_VALUE As Long = -1
    Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10
    
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    
    
    Private Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
    End Type
    
    
    
    Private Declare Function FindFirstFile Lib "kernel32" _
    () '                                       Alias "FindFirstFileA" _
                                           (ByVal lpFileName As String, _
                                            lpFindFileData As WIN32_FIND_DATA) As Long
    
    Private Declare Function FindClose Lib "kernel32" _
    () '                                   (ByVal hFindFile As Long) As Long
    
    
    
    Public Function FolderExists(sFolder As String) As Boolean
    
        Dim hFile As Long
        Dim WFD As WIN32_FIND_DATA
    
        'remove training slash before verifying
        sFolder = UnQualifyPath(sFolder)
    
        'call the API pasing the folder
        hFile = FindFirstFile(sFolder, WFD)
    
        'if a valid file handle was returned,
        'and the directory attribute is set
        'the folder exists
        FolderExists = (hFile <> INVALID_HANDLE_VALUE) And _
                       (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY)
    
        'clean up
        Call FindClose(hFile)
    
    End Function
    
    Private Function UnQualifyPath(ByVal sFolder As String) As String
    
    'trim and remove any trailing slash
        sFolder = Trim$(sFolder)
    
        If Right$(sFolder, 1) = "\" Then
            UnQualifyPath = Left$(sFolder, Len(sFolder) - 1)
        Else
            UnQualifyPath = sFolder
        End If
    
    End Function
    i then swapped to

    Code:
    Private Declare Function PathIsDirectory Lib "shlwapi.dll" Alias "PathIsDirectoryA" _
    (ByVal pszPath As String) As Long
    
    
    Public Function FolderExists(ByVal sFolder As String) As Boolean
        FolderExists = PathIsDirectory(sFolder)
    End Function
    and it works fine.

    Why would the first example now say False if case is incorrect but the folder path is correct.

    tks

  2. #2
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,619

    Re: FolderExists Issue

    Code:
    Public Function FolderExists(sFolder As String) As Boolean
    Dim FolderAttributes As Long
        FolderAttributes = GetFileAttributesW(StrPtr(sFolder))
        If FolderAttributes <> INVALID_HANDLE_VALUE Then FolderExists = (FolderAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY
    End Function

  3. #3
    Hyperactive Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    476

    Re: FolderExists Issue

    I like to use the SHParseDisplayName or SHCreateItemFromParsingName API for this. If the return value is not equal to S_OK, the path does not exist. Another advantage of the API is that it can handle paths that do not have a drive letter.
    Last edited by -Franky-; Today at 01:17 AM.

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