Results 1 to 40 of 44

Thread: VB - List All The Files In A Directory

Hybrid View

  1. #1
    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.

  2. #2
    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

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