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