devxlogo

Using the dir() and the dir$() Functions

Using the dir() and the dir$() Functions

It’s possible to use the dir() and the dir$() functions to list directories. However, many programmers handle the two first returns from the functions “.” and “..” by using an if statement or a select case within the loop:

 Public Function ListDirs(Path As String) As String  Dim sTemp As String        sTemp = Dir$(Path & "*.*", vbDirectory)        Do While Len(sTemp)            If sTemp  "." Or sTemp  ".." Then                If GetAttr(Path & "" & sTemp) _                                      And vbDirectory Then                    ListDirs = ListDirs & sTemp & ";"                End If            End If            sTemp = Dir$()        Loop    End Function

For an faster loop, delete the If-statement from the loop and ignore the first and second value if the first is a “.”:

     Public Function ListDirs(Path As String) As String    Dim sTemp As String        sTemp = Dir$(Path & "*.*", vbDirectory)        If sTemp = "." Then            Dir$            sTemp = Dir$        End If        Do While Len(sTemp)            If GetAttr(Path & "" & sTemp) And _                                      vbDirectory Then                ListDirs = ListDirs & sTemp & ";"            End If            sTemp = Dir$()        Loop    End Function
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist