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

devx-admin

Share the Post: