Results 1 to 7 of 7

Thread: Listing all Folders in a folder or drive...

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Location
    a
    Posts
    12
    I need to know how to list all folders in a folder or drive. I am making a list program that makes a list of all the files or folders in a dir, and converting them to proper case. Example:

    C:\dos
    C:\windows
    C:\ati

    will make a list like...

    Dos
    Windows
    Ati

    I already have the formatting and files done, but i have tried forever to get the folders and I just can't seem to get them to work, any suggestions.. thanks for your time!

    - AZPC
    ICQ: 25122538
    E-mail: [email protected]

  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    Red face

    We'd use a file list box instead of trying to code for this yourself. It's faster and easier.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Location
    a
    Posts
    12

    how?

    how do i do that, and get each dir as text
    - azpc

  4. #4
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    Question

    Ok, I think I might understand now. Try this:

    Sub ListDirs()
    Dir1.Path = "c:\windows"
    For i = -1 To Dir1.ListCount
    Text1.Text = Text1.Text & Dir1.List(i) & vbCrLf
    Next

    End Sub

  5. #5
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Drop a command button, two list boxes onto a form. Paste the following codes into the Declaration section and run it. I recommend you change the path to a smaller path than "C:\". If you leave the path as it is, it will list all folders on your computer.

    When you click on the command button, it will take 2 to 3 seconds depending on the total number of folders you have for that directory.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
      ListSubDirs "C:\"
    End Sub
    
    Private Sub List1_Click()
      Call ListFiles(List1.Text & "\")
    End Sub
    
    Private Sub ListFiles(strPath)
        List2.Clear
        On Error Resume Next
        Dim strDir
        strDir = Dir(strPath)
        Do While strDir <> ""
          If strDir <> "." And strDir <> ".." Then
              List2.AddItem strDir
          End If
          strDir = Dir
        Loop
    End Sub
    
    Private Sub ListSubDirs(Path)
        On Error Resume Next
        Dim intCount As Integer
        Dim intX As Integer
        Dim arrayV()
        Dim strDir As String
        
        strDir = Dir(Path, vbDirectory)
        Do While strDir <> ""
          'PURPOSE: Don't process parent or current directory entry so process it
          If strDir <> "." And strDir <> ".." Then
            If GetAttr(Path + strDir) = vbDirectory Then
              ' This is a directory
              If (intCount Mod 10) = 0 Then
                ' Resize the array
                ReDim Preserve arrayV(intCount + 10)
              End If
              intCount = intCount + 1
              
              'PURPOSE: Add directory name to array
              arrayV(intCount) = strDir
            End If
          End If
          strDir = Dir
        Loop
        
        For intX = 1 To intCount
            'PURPOSE: Directory's name in the list box.
            List1.AddItem Path & arrayV(intX)
            'PURPOSE: Recursive call
            ' Find this directory's sub-directories
            ListSubDirs Path & arrayV(intX) & "\"
        Next intX
        
        DoEvents
    End Sub
    Chemically Formulated As:
    Dr. Nitro

  6. #6
    Guest
    hey guys, instead of all this code why not check out the filesystemobject (FSO)

    it lets you treat each folder like an individual collection with the ability to do a [for...each...next loop] to traverse the folder!
    check it out

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Then you can use Rename, Ucase and left to fix the names

Posting Permissions

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



Click Here to Expand Forum to Full Width