Results 1 to 15 of 15

Thread: [RESOLVED] Dir$ issue

  1. #1

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,992

    Resolved [RESOLVED] Dir$ issue

    Want to simply use Dir$ (instead of FSO) to determine if a folder exists. Having a bit of an issue here:

    (tried it with and without trailing slash)

    Code:
    Dim myDir As String
        Dim sArtist as String
        sArtist = "ABBA"
        Debug.Print App.path & "\Music\" & sArtist & "\"    'works
    
    
        myDir = Dir$(App.path & "\Music\" & sArtist & "\")
        Debug.Print myDir  'does NOT work
    When I run this, I get the following in the Immediate Window:

    C:\Users\myUserName\Documents\A-Visual Basic Programs\JukeBox-2018\Music\ABBA\ (for the first debug.print)

    I do not get anything the the second Debug.Print...IOW, Dir$ seems to be returning a "", even though the directory does exist.

    What am I missing here?
    Sam I am (as well as Confused at times).

  2. #2

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,992

    Re: Dir$ issue

    EDIT...sorta --- reading Help, I added ",VbDirectory to the Dir$ call, like this:

    Code:
    myDir = Dir$(App.path & "\Music\" & sArtist & "\", vbDirectory)
    What I get in debug is "."
    Sam I am (as well as Confused at times).

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,702

    Re: Dir$ issue

    Code:
    Option Explicit
    
    Private Sub Main()
        Const PATH As String = "\SomeFolder"
        Dim Attr As VbFileAttribute
    
        On Error Resume Next
        Attr = GetAttr(App.PATH & PATH)
        If Err Then
            On Error GoTo 0
            MsgBox "Does not exist"
        Else
            On Error GoTo 0
            If Attr And vbDirectory Then
                MsgBox "Exists"
            Else
                MsgBox "No folder, but a file exists"
            End If
        End If
    End Sub

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,702

    Re: Dir$ issue

    When you request a list of directories this way, you will get the relative directories "." and ".." as expected.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,671

    Re: Dir$ issue

    If memory serves it returns an empty string when it does not exist

  6. #6

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,992

    Re: Dir$ issue

    MSDN doesn't seem to state that (at least not clearly). I'll use the "Attribute method" you posted (thanks for that), but CAN you get 'sub' directories (below . and ..) by calling it over and over???? Anyway, going to close the thread (well, mark it as Resolved, anyway), but others may also have the same question about using DIR for directories...
    Sam I am (as well as Confused at times).

  7. #7

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,992

    Re: Dir$ issue

    @ DM, it does...BUT, in my case, the directory DOES exist. So, just confusing, I guess.
    Sam I am (as well as Confused at times).

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,671

    Re: [RESOLVED] Dir$ issue

    Code:
    Private Function FolderExists(PathName As String) As Boolean
        FolderExists = Dir$(PathName, vbDirectory) <> ""
    End Function

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,645

    Re: [RESOLVED] Dir$ issue

    Code:
    
    Public Function bFolderExists(sDir As String) As Boolean
        Dim s As String
        s = sDir
        If Right$(s, 1) = "\" Then s = Left$(s, Len(s) - 1)
        On Error GoTo FileExistsError
        ' If no error then something existed.
        bFolderExists = ((GetAttr(s) And vbDirectory) = vbDirectory)
        Exit Function
    FileExistsError:
        bFolderExists = False
    End Function
    
    
    That's mine, but it looks like DataMiser might have a simpler version. I haven't looked at that code in decades, but it's reliably working everyday.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,702

    Re: [RESOLVED] Dir$ issue

    Code:
    Option Explicit
    
    Private Const INVALID_FILE_ATTRIBUTES As Long = -1
    
    Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesW" ( _
        ByVal lpFileName As Long) As VbFileAttribute
    
    Private Function Exists(ByRef FileName As String) As Boolean
        Exists = GetFileAttributes(StrPtr(FileName)) <> INVALID_FILE_ATTRIBUTES
    End Function
    
    Private Function ExistsDirectory(ByRef FileName As String) As Boolean
        Dim VbFileAttribute As VbFileAttribute
    
        VbFileAttribute = GetFileAttributes(StrPtr(FileName))
        If VbFileAttribute <> INVALID_FILE_ATTRIBUTES Then
            ExistsDirectory = VbFileAttribute And vbDirectory
        End If
    End Function
    Works with or without trailing backslash no need to fiddle. Unicode aware as a bonus.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,426

    Re: [RESOLVED] Dir$ issue

    @ sam
    if you just want a list of folders below some given folder your code in #2 is correct, just add all the folders returned, i am sure that any of the codes posted above can be used to confirm that the file is a folder as files will be returned as well as folders, to an array or collection, avoid . or .., which will be the first 2 folders returned
    yes you can make the procedure recursive and find all subfolders that exist at any level below a specified folder
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,992

    Re: [RESOLVED] Dir$ issue

    yes you can make the procedure recursive and find all subfolders that exist at any level below a specified folder
    Aye (Irish, not a pirate), I thought so, just never used it before to do recursive searches.

    all good responses, ya'll (also southern accent picked up over last 29 years)...thanks
    Sam I am (as well as Confused at times).

  13. #13
    Member
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    47

    Re: [RESOLVED] Dir$ issue

    Perhaps belatedly, but still just another one that is unicode aware:

    Code:
    Private Declare Function PathFileExistsW Lib "shlwapi" (ByVal pszPath As Long) As Boolean
    Private Declare Function PathIsDirectoryW Lib "shlwapi" (ByVal pszPath As Long) As Boolean
    
    
    Public Function PathExists(ByVal sPath As String) As Boolean
    ' checks existance of a given path - returns true for an existing filename OR! directory
      PathExists = PathFileExistsW(StrPtr(sPath))
    End Function
    
    Public Function DirExists(ByVal sPath As String) As Boolean
      DirExists = PathIsDirectoryW(StrPtr(sPath))
    End Function
    
    Public Function FileExists(ByVal sPath As String) As Boolean
      If Not DirExists(sPath) Then FileExists = PathFileExistsW(StrPtr(sPath))
    End Function
    PS: prepending "\\?\UNC" for a network path or "\\?" for a local path also makes these functions LFN (Long FileName-) aware.
    Last edited by Zphere; Aug 6th, 2021 at 06:49 AM.

  14. #14
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,195

    Re: [RESOLVED] Dir$ issue

    In DirExists what's the point of first checking if PathExists before calling PathIsDirectoryW API?

    Btw, directly assigning PathExists = PathFileExistsW(...) saves an If statement and turns both functions into (the mythical) one-liners.

    cheers,
    </wqw>

  15. #15
    Member
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    47

    Re: [RESOLVED] Dir$ issue

    wqweto,

    PathFileExistsW does not distinguish between existance of a filename or a directory.
    As its name suggest, it simply checks for existance of a path.

    Zphere

    EDIT: Sorry, at second glance I see that I misunderstood your intent.
    I corrected my first post.
    Last edited by Zphere; Aug 6th, 2021 at 06:47 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