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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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