Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Apr 12, 2002

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
Andreas Hillqvist
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date