Results 1 to 5 of 5

Thread: Q: How to load file contents of a directory into a listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    12
    Anyone out there know how to load the file contents of a directory into a list box? I also need the files to be restricted to one or two extensions (.gif and .jpg)
    Thanks
    ADrift

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    You need to use the "Dir" command.

    Code:
    sNext = Dir("C:\Program Files",vbNormal)
    Do while sNext <> ""
      If InStr("|.gif|.jpg|", "|" & LCase$(Mid$(sNext,InStrRev(sNext,".")))) > 0 Then
        ListBox.AddItem sNext
      End If
      sNext = Dir
    Loop
    Something like that.... just off the top of my head

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    12
    That didn't work;
    The listbox just came up empty,although the code did execute properly.
    Any Other Ideas?
    Thanks
    Ari

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Why not use the FileListBox control - you can set the pattern property of that to "*.jpg;*.gif", and set the path of it to wherever you want it to look.
    Otherwise it behaves pretty much like a normal listbox.

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  5. #5
    Guest

    Thumbs up

    The reason it came up empty is because you may have nothing in the directory. Or you forgot the \ .

    Code:
    sNext = Dir("C:\My Documents\",vbNormal)
    Do while sNext <> ""
      If InStr("|.gif|.jpg|", "|" & LCase$(Mid$(sNext,InStrRev(sNext,".")))) > 0 Then
        ListBox.AddItem sNext
      End If
      sNext = Dir
    Loop
    It works fine for me. Good code Gen-X .

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