Results 1 to 40 of 44

Thread: VB - List All The Files In A Directory

Hybrid View

  1. #1

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  2. #2
    New Member
    Join Date
    Jun 2007
    Posts
    2

    Thumbs up Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub

    Hello,

    Thank for the above code, works a treat but I'm just wondering if anyone could help me out with a similarly easy way to do the same but to list the names of directories ("folders") within a directory?

    So, the above code works great, it lists all the files of whatever type in say "C:\folder1".

    What I need is code to list all the directories within "C:\folder1", not including subdirectories.

    If anyone could help I would be much obliged.

    Thanks in advance.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by RedJam
    What I need is code to list all the directories within "C:\folder1", not including subdirectories.
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const LB_DIR = &H18D
    
    Private Const DDL_DIRECTORY = &H10
    Private Const DDL_ARCHIVE = &H20
    Private Const DDL_EXCLUSIVE = &H8000
    
    Private Sub Command1_Click()
        List1.Clear
        SendMessage List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_DIRECTORY, ByVal "C:\folder1\*.*"
    End Sub

  4. #4
    New Member
    Join Date
    Oct 2009
    Posts
    2

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11 View Post
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub
    Quote Originally Posted by goldenix View Post
    just a note that none of the codes work in 2008.
    this code is pretty close, the following is what i turned it into and used, in vb.net 2008:

    Code:
    Dim file As String
            file = Dir$("D:\Documents and Settings\Compaq_Owner\My Documents\My Pictures\cards\*.png")
            Do While Len(file)
                ListBox2.Items.Add(Mid(file, 1, InStr(file, ".") - 1))
                file = Dir$()
            Loop

  5. #5
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11 View Post
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub
    why do i get errors?
    i added a list box named it List1

    Error 1 Optional parameters must specify a default value. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 3 74 WindowsApplication1

    Error 2 'Public ReadOnly Property Right() As Integer' has no parameters and its return type cannot be indexed. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 7 12 WindowsApplication1

    Error 4 'Public Property Left() As Integer' has no parameters and its return type cannot be indexed. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 11 16 WindowsApplication1

    Error 5 Type character '$' does not match declared data type 'Integer'. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 11 16 WindowsApplication1

    Error 6 'AddItem' is not a member of 'System.Windows.Forms.ListBox'. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 17 13 WindowsApplication1

  6. #6
    New Member
    Join Date
    Mar 2011
    Posts
    15

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by manavo11 View Post
    The Dir way :

    VB Code:
    1. 'Just add a listbox (List1)
    2.  
    3. Private Sub ListFiles(strPath As String, Optional Extention As String)
    4. 'Leave Extention blank for all files
    5.     Dim File As String
    6.    
    7.     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
    8.    
    9.     If Trim$(Extention) = "" Then
    10.         Extention = "*.*"
    11.     ElseIf Left$(Extention, 2) <> "*." Then
    12.         Extention = "*." & Extention
    13.     End If
    14.    
    15.     File = Dir$(strPath & Extention)
    16.     Do While Len(File)
    17.         List1.AddItem File
    18.         File = Dir$
    19.     Loop
    20. End Sub
    21.  
    22. Private Sub Form_Load()
    23. ListFiles "C:\", "txt"
    24. End Sub
    I'm attempting to use this code, it should do everything I need, but my Listbox comes up blank. Which was my problem before and hoped this code would help me fix this. I'm teaching myself how to use VB6 so please spell things out to me as though I were a little kid. Thank you.

  7. #7

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB - List All The Files In A Directory

    Quote Originally Posted by davis1d0 View Post
    I'm attempting to use this code, it should do everything I need, but my Listbox comes up blank. Which was my problem before and hoped this code would help me fix this. I'm teaching myself how to use VB6 so please spell things out to me as though I were a little kid. Thank you.
    Unless we see exactly your code, we won't be able to help! You can create a new thread in the Visual Basic 6 and Earlier forum to get more help


    Has someone helped you? Then you can Rate their helpful post.

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