Results 1 to 11 of 11

Thread: FolderExists Issue

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    805

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

    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-; Jun 4th, 2026 at 01:17 AM.

  4. #4
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,002

    Re: FolderExists Issue

    also FindFirstFileA is the old one, u should use FindFirstFileW.
    I use FindFirstFileA only in my own folders that I know is VB6 friendly.
    the folder is shortpath converted as well.

    but, if u deal with unknown folders, u need to use Unicode aware.

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

    Wink Re: FolderExists Issue

    And of course the WinRT solution:
    Code:
    Public Function FolderExists(sFolder As String) As Boolean
        With New cAwait
            FolderExists = .Await(StorageFolderStatics.GetFolderFromPathAsync(StrRef(sFolder))) = AsyncStatus_Completed
        End With
    End Function
    Doesn't block the thread in case of unreachable network paths!

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,671

    Re: FolderExists Issue

    Quote Originally Posted by VanGoghGaming View Post
    And of course the WinRT solution:
    Code:
    Public Function FolderExists(sFolder As String) As Boolean
        With New cAwait
            FolderExists = .Await(StorageFolderStatics.GetFolderFromPathAsync(StrRef(sFolder))) = AsyncStatus_Completed
        End With
    End Function
    Doesn't block the thread in case of unreachable network paths!
    But that can be also a problem if you don't want that, because you can receive events. Similar to having a DoEvents.

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

    Wink Re: FolderExists Issue

    It's not similar to having a DoEvents, it is exactly that!

    Generally it is preferred to have a responsive UI rather than a frozen one but it is up to the developer to manage reentrancy problems. One major advantage is that it allows the user to cancel pending operations.

    If you use an existing instance of the cAwait class, it is smart enough not to initiate another async operation while a previous one is still being awaited and the Cancel method can be used to abort the current async operation.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    805

    Re: FolderExists Issue

    i have noticed though that in the same app i changed the folderexists, i came across another issue.

    I used the following (part of code) to check a file exists, if yes then pass to CImageInfo.



    Code:
     If FileExists(MyData.Fanart) = True Then
                Dim AA As New CImageInfo
                AA.ReadImageInfo MyData.Fanart

    MyData.Fanart = "C\xxx\xxx\Fanart.jpg" (created in code) and the Fileexists finds it so passes on to the following function. but it errors out at m_Size = ConvertBytes(FileLen(sFileName), 2, True)
    with path not found. In the folder is fanart.jpg and if i change the lowercase f to uppercase F and rerun the code works fine. as soon as i put it back to lower case f it fails.

    Code:
    Public Sub ReadImageInfo(sFileName As String)
    
    ' This is the sub to call to retrieve information on a file.
    
    ' Byte array buffer to store part of the file
        Dim bBuf(BUFFERSIZE) As Byte
        ' Open file number
        Dim iFN As Integer
    
        ' Set all properties to default values
        m_Width = 0
        m_Height = 0
        m_Depth = 0
        m_ImageType = itUNKNOWN
    
        ' here we will load the first part of a file into a byte
        'array the amount of the file stored here depends on
        'the BUFFERSIZE constant
        iFN = FreeFile
        Open sFileName For Binary As iFN
        Get #iFN, 1, bBuf()
        Close iFN
    
        'get filesize of image
        m_Size = ConvertBytes(FileLen(sFileName), 2, True) ' fails if filename is not the exact upper and lower case.
    
    'more code her etc
    any one have any ideas why this would suddenly start happening. is MS being more strict. ?

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,734

    Re: FolderExists Issue

    Are your api declarations really this
    Code:
    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

  10. #10
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,671

    Re: FolderExists Issue

    Quote Originally Posted by VanGoghGaming View Post
    It's not similar to having a DoEvents, it is exactly that!

    Generally it is preferred to have a responsive UI rather than a frozen one but it is up to the developer to manage reentrancy problems. One major advantage is that it allows the user to cancel pending operations.

    If you use an existing instance of the cAwait class, it is smart enough not to initiate another async operation while a previous one is still being awaited and the Cancel method can be used to abort the current async operation.
    Some days ago I was debugging an issue in a program. It turned out that in the Form_Load I used a function that ended using another function that I was not aware that it worked async. It had been made by Claude:

    Code:
    '==========================================================================
    ' Run a VBScript in hidden mode and reads the first line of the output.
    '==========================================================================
    Private Function RunVBSAndGetResult(ByVal iTempPath As String, ByVal iResultPath As String) As String
        Dim wsh As Object, fso As Object, ts As Object
        Set wsh = CreateObject("WScript.Shell")
        Set fso = CreateObject("Scripting.FileSystemObject")
        wsh.Run "wscript " & VBSQuote(iTempPath), 0, True   ' 0=hidden, True=wait
        RunVBSAndGetResult = ""
        If fso.FileExists(iResultPath) Then
            Set ts = fso.OpenTextFile(iResultPath, 1)       ' 1 = ForReading
            If Not ts.AtEndOfStream Then RunVBSAndGetResult = Trim(ts.ReadLine)
            ts.Close
        End If
    End Function
    To fix that issue that I was having, I moved that code that was in Form_Load to a timer. It needed to run when program starts but not necessarily in the Form_Load.
    I mean, one needs to be aware of the async calls, they change the game.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    805

    Re: FolderExists Issue

    Quote Originally Posted by Arnoutdv View Post
    Are your api declarations really this
    Code:
    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
    the fileexists and folder exists is working fine.

    I have created a bare bones example

    Create a New Project Form 1 and add a Class CImageInfo

    in the class paste

    Code:
    Option Explicit
    
    Private m_Size As String
    
    Public Property Get Size() As String
        Size = m_Size
    End Property
    
    Public Sub ReadImageInfo(sFileName As String)
    
    m_Size = FileLen(sFileName)
    End Sub
    and in the form paste , and add button1

    Code:
    Private Sub Command1_Click()
    
        Dim AA As New CImageInfo
        AA.ReadImageInfo "C:\xxxxx\fanart.jpg"
        Debug.Print AA.Size
        Set AA = Nothing
    
    End Sub
    change the path to a valid jpg image. Mine is on a qnap Nas, (maybe thats the issue ?)

    when i click button and filename is fanart.jpg it works , if its Fanart.jpg it fails with path error.

    I recently updated my nas firmware and it could be that , what is causing the issue.

    Any hlp appreciated

    Edit:
    Just tried the jpg on my desktop and either upper or lower case the image is found.
    It appears to me the Nas TS-X73A that is causing the issue with filename case.

    not sure how to fix that. ? any ideas
    Last edited by k_zeon; Yesterday at 12:37 PM.

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