dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Tip: Directory Listing

Hybrid View

  1. #1
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Tip: Directory Listing

    Okay, i puzzled over this for some time, and i have finally worked out:

    How To List the Contents of a Directory, without using Scripting.FileSystemObject

    Dir() and Dir$() are most often used to check if a file exists:

    Dir("c:\existingFile.txt", vbNormal)
    -> "c:\existingFile.txt"
    Dir("c:\nonExistingFile.txt", vbNormal)
    -> ""

    But if you pass in some kind of wildcard, or directory name, together with the attributes you want, Then repeatedly call Dir without any parameters, it lists the entire directory:

    Dir("C:\", vbDirectory)
    -> .
    Dir()
    -> ..
    Dir()
    -> "My Documents"
    Dir()
    -> "Program Files"
    Dir()
    -> "Windows"
    Dir()
    -> ""

    and returns a zero length string at the end of the listing

    so you can capture this into an array, and redim preserve array(ubound(array)+1) it as many times as needed

    _________________________________________________

    for implementations requiring listing of many files, it might be advisable to either:

    create a counter
    Dir() until "" is obtained, incrementing the counter each time
    create an array of length(counter)
    repeat the Dir() and fill the array

    OR

    use a linked list

    ______________________________________

    either method is guaranteed to be faster than re-dimming an array a few thousand times
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  2. #2
    Join Date
    Apr 2002
    Location
    Los Angeles, Ca
    Posts
    238
    Cjard ...
    If you happen to have that in a small working project, I would love to see all the source code, so that maybe I can wrap my mind around it a little better.

  3. #3
    Join Date
    Nov 2003
    Location
    Seattle, WA
    Posts
    265
    Is this what you mean cjard. I never really used the dir function before. I always use to use the FileSystemObject.

    But here a sampe for Hobbit K
    This class gets the listing of any directory specified, and returns its findings as a Variant array.

    Code:
    Private strPath As String
    
    Public Property Get Path() As String
        Path = strPath
    End Property
    
    Public Property Let Path(ByVal vNewValue As String)
        strPath = vNewValue
    End Property
    
    Public Function GetListing() As Variant
        Dim j As Integer, i As Integer
        Dim strListing() As String
        
        If Len(strPath) = 0 Then MsgBox "Error: No Directory Specified!": Exit Function
        
        temp$ = Dir(strPath, vbDirectory)
        i = Len(temp$)
        If i = 0 Then Exit Function
        j = 1
        
        While i > 0
            temp$ = Dir()
            i = Len(temp$)
            j = j + 1
        Wend
        
        ReDim strListing(j)
        
        strListing(1) = Dir(strPath, vbDirectory)
        
        For i = 2 To j - 1 Step 1
            strListing(i) = Dir()
            If InStr(1, strListing(i), ".") = 0 Then
                strListing(i) = "[" & strListing(i) & "]"
            End If
        Next
        
        GetListing = strListing()
    End Function
    Last edited by Jinto; December 20th, 2003 at 12:23 AM.

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    Originally posted by Hobbit K
    Cjard ...
    If you happen to have that in a small working project, I would love to see all the source code, so that maybe I can wrap my mind around it a little better.
    here is a quick and dirty bit of code then, not a full project:

    Code:
      Dim tempStr as String
      Dim path as String
      Dim allNames as String
      Dim dirList() As String
    
      path = "c:\windows"
    
      'lets list all the directories inside of C:\WINDOWS folder
      tempStr = FileSystem.Dir$(path, vbDirectory)
      
      'check if the path is valid. tempStr will be "" if path invalid
      If tempStr = "" Then
        MsgBox "Cannot find a folder called:" & vbCrLf & vbCrLf & path, vbOKOnly, "Missing folder", , vbInformation
    
      Else
        'get the dir listing - here the call to BLANK Dir$() is crucial
        While tempStr <> ""
          allNames = allNames & tempStr & vbCrLf
          tempStr = Dir$()
        Wend
    
        'put the paths into an array
        dirList = Split(allNames, vbCrLf)
    there are many ways of getting the names into the array, you could re-dim it every time. i discourage this approach as it will impact performance on long directories. Linked List would perform best. I do not know how Split works, exactly, but suspect it is better than Redimming many times.
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    note that with this method, there is a blank entry at the end of the dirList() array, because Split() treats the trailing vbCrLf as a "delimiter" between the LAST NAME, and a blank string (dumb, but logical). You can trim the last vbCrLf somehow.. i tried Trim$() but it only seems to trim spaces, not all whitespace
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)