Results 1 to 18 of 18

Thread: add all files from a folder to a listbox?

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    32

    add all files from a folder to a listbox?

    can anyone help me with this? ive been looking at examples and trying to figure it out for 2 hours....

  2. #2
    New Member
    Join Date
    May 2009
    Posts
    13

    Re: add all files from a folder to a listbox?

    there are three controls can do this , the first is DriveListBox control ,the second is DirListBox control. the last one is FileListBox. if you want get all the file name ,you need link all of them as a combox. you need add some code with the change event.
    you can get more information by search with MSDN.

  3. #3
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: add all files from a folder to a listbox?

    What's the purpose? It's pretty easy to show the commondialog control's file browser.

    Otherwise, the controls mentioned above are in the toolbox.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    32

    Re: add all files from a folder to a listbox?

    i'm trying to make it so that when the program start it reads the title of all the music files in my "music folder" and displays the titles in a list...so those tools don't really help me...

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: add all files from a folder to a listbox?

    Quote Originally Posted by hans4 View Post
    i'm trying to make it so that when the program start it reads the title of all the music files in my "music folder" and displays the titles in a list...so those tools don't really help me...
    Actually the FileList control can help you. The only thing you really need to know is where "my music" is located. There is an API approach that will find that special folder regardless of where it is physically. In this example shows how to retrieve all special folders (as of the date of its writing). You can trim it down to a few lines of code if looking for just one folder.

    Once you have the folder location, set the Path property of the filelist control. You can also use a listbox and manually fill it by using Dir() in a loop, or using APIs to fill the listbox for you. Quite a few ways to skin that cat.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: add all files from a folder to a listbox?

    yes, the file list box could work fine for you. You just have to tell it which directory to look at.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  7. #7
    New Member Eaglecollector's Avatar
    Join Date
    Jun 2009
    Posts
    6

    Re: add all files from a folder to a listbox?

    Most of this I got from AllApi.net Seemed to be the easiest way to locate the My Pictures folder.
    To test this start a new project and add a Directory List Box, a File List Box and a Regular List Box to the form. Copy and past the following code.

    Option Explicit
    Const CSIDL_MYMUSIC = &HD

    Private Type SHITEMID
    cb As Long
    abID As Byte
    End Type

    Private Type ITEMIDLIST
    mkid As SHITEMID
    End Type

    Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long

    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long

    Private Sub Form_Load()
    'Add a Directory List box to your form
    'Add a File List Box to your form
    'Add a regular List Box to your form
    Dim i As Integer
    Dir1.Visible = False
    File1.Visible = False
    Dir1.Path = GetSpecialfolder(CSIDL_MYMUSIC)
    File1.Path = Dir1.Path
    For i = 0 To File1.ListCount - 1
    List1.AddItem File1.List(i)
    Next i
    End Sub

    Private Function GetSpecialfolder(CSIDL As Long) As String
    Dim Path As String
    Dim NOERROR As Boolean
    Dim r As Long
    Dim IDL As ITEMIDLIST
    'Get the special folder
    r = SHGetSpecialFolderLocation(100, CSIDL, IDL)
    If r = NOERROR Then
    'Create a buffer
    Path = Space(512)
    'Get the path from the IDList
    r = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal Path$)
    'Remove the unnecessary chr$(0)'s
    GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1)
    Exit Function
    End If
    GetSpecialfolder = ""
    End Function

  8. #8
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: add all files from a folder to a listbox?

    This does it:
    Code:
    Private Sub Command1_Click()
    Dim NameFile As String, SubDir As String
    SubDir = CurDir$ & "\*.*" ' Change CurDir to whatever directory is being searched
    NameFile = Dir$(SubDir)
    Do While NameFile <> vbNullString
        List1.AddItem NameFile
        NameFile = Dir$
    Loop
    End Sub
    Doctor Ed

  9. #9
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: add all files from a folder to a listbox?

    How about an API solution...
    Code:
    Option Explicit
    
    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_READWRITE = &H0
    Private Const DDL_READONLY = &H1
    Private Const DDL_HIDDEN = &H2
    Private Const DDL_SYSTEM = &H4
    Private Const DDL_DIRECTORY = &H10
    Private Const DDL_ARCHIVE = &H20
    Private Const DDL_DRIVES = &H4000
    Private Const DDL_EXCLUSIVE = &H8000&
    Private Const DDL_POSTMSGS = &H2000
    
    Private Const FindAllFiles = DDL_READWRITE Or DDL_READONLY Or DDL_HIDDEN Or DDL_SYSTEM
    
    Private Sub Form_Load()
    Dim Cnt As Integer
    
    Me.Visible = True
    
    Cnt = SendMessage(List1.hwnd, LB_DIR, FindAllFiles, ByVal "c:\*.*")
    
    MsgBox "Found " & Cnt + 1 & " files"
    
    End Sub
    Good Luck
    Option Explicit should not be an Option!

  10. #10
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: add all files from a folder to a listbox?

    Quote Originally Posted by vb5prgrmr View Post
    How about an API solution...
    Code:
    Option Explicit
    
    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_READWRITE = &H0
    Private Const DDL_READONLY = &H1
    Private Const DDL_HIDDEN = &H2
    Private Const DDL_SYSTEM = &H4
    Private Const DDL_DIRECTORY = &H10
    Private Const DDL_ARCHIVE = &H20
    Private Const DDL_DRIVES = &H4000
    Private Const DDL_EXCLUSIVE = &H8000&
    Private Const DDL_POSTMSGS = &H2000
    
    Private Const FindAllFiles = DDL_READWRITE Or DDL_READONLY Or DDL_HIDDEN Or DDL_SYSTEM
    
    Private Sub Form_Load()
    Dim Cnt As Integer
    
    Me.Visible = True
    
    Cnt = SendMessage(List1.hwnd, LB_DIR, FindAllFiles, ByVal "c:\*.*")
    
    MsgBox "Found " & Cnt + 1 & " files"
    
    End Sub
    Good Luck
    that's actually the code i was looking for. I'd seen it on this site before.

    Note to user: This is to be used with a standard listbox, not the file or dir one. In reality they are all the same control and this treats a standard listbox as a file box.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  11. #11
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: add all files from a folder to a listbox?

    Yeah, I posted it about a month or so ago and couldn't find it myself with a keyword search so I posted this version.

    Good Luck
    Option Explicit should not be an Option!

  12. #12
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: add all files from a folder to a listbox?

    Quote Originally Posted by vb5prgrmr View Post
    Yeah, I posted it about a month or so ago and couldn't find it myself with a keyword search so I posted this version.

    Good Luck
    you don't need a keyword search if you post it yourself. Your user profile has a link "find all posts started by vb5prgrmr" and you can also view all threads you are subscribed to.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  13. #13
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: add all files from a folder to a listbox?

    Quote Originally Posted by Lord Orwell View Post
    you don't need a keyword search if you post it yourself. Your user profile has a link "find all posts started by vb5prgrmr" and you can also view all threads you are subscribed to.
    Lord Orwell, please permit me to ask a question.

    Why would any programmer want to use an API to do what 7 lines of typical code in VB6 (or VB3 for that matter) can probaby do easier and faster?

    Just curious.
    Doctor Ed

  14. #14
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: add all files from a folder to a listbox?

    Quote Originally Posted by Code Doc View Post
    Lord Orwell, please permit me to ask a question.

    Why would any programmer want to use an API to do what 7 lines of typical code in VB6 (or VB3 for that matter) can probaby do easier and faster?

    Just curious.
    Because the API is actually faster because it is a direct call to the underlying functionality of most of vb's functionality, which means it is not interpreted by the visual basic runtime and then passed to the underlying functionality and the results pumped back up through the chain like the six lines of dir you are talking about.

    Lord Orwell, as of so far I have 13 threads that I replied to with sendmessage in it and 345 threads in all that I have replied to. And yes I know I could do that but when you are looking for a previously posted solution based upon memory without trying to open up another application. Well sometimes you have to open up that other application to remind yourself...

    Good Luck All
    Option Explicit should not be an Option!

  15. #15
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: add all files from a folder to a listbox?

    Quote Originally Posted by Code Doc View Post
    Lord Orwell, please permit me to ask a question.

    Why would any programmer want to use an API to do what 7 lines of typical code in VB6 (or VB3 for that matter) can probaby do easier and faster?

    Just curious.
    a couple of reasons.

    1. There isn't any other solution for getting the location of the user's folder in question. Yes, there are default locations, but these locations aren't even in the same places in different operating systems. Your desktop location for example:
    win95/98/me:
    c:\windows\desktop (assuming they name their install windows)
    2k/xp:
    c:\documents and settings\users\username\desktop
    vista\7:
    c:\users\username\desktop

    and these are merely default locations. They can be moved by the user to anywhere they wish, even other hard drives.

    2. The filelist control is an activex control and if you use it instead of the standard listbox, it has to be included with the compiled program, making the distributable larger. This is especially apparent with the common dialog control. the control is a couple of megabytes in size, and about 20 lines of code can reproduce it.

    3. Using the controls through api can give you more functionality than the control itself gives you through vb. Richedit comes to mind. It has link functionality that isn't used in the richtextbox control.

    4. The code is actually smaller. If you strip down that code posted above, it's actually a declaration and a call and that's about it. None of the constants are required.
    Code:
    Cnt = SendMessage(List1.hwnd, &H18d, 7, ByVal "c:\*.*")
    besides the single sendmessage declaration, that's all that is required to load the c:\ directory into the listbox, and it's much, MUCH faster than the dir$ command in a loop.
    Last edited by Lord Orwell; Jun 23rd, 2009 at 02:34 AM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  16. #16
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: add all files from a folder to a listbox?

    Quote Originally Posted by Lord Orwell View Post
    a couple of reasons.

    1. There isn't any other solution for getting the location of the user's folder in question. Yes, there are default locations, but these locations aren't even in the same places in different operating systems. Your desktop location for example:
    win95/98/me:
    c:\windows\desktop (assuming they name their install windows)
    2k/xp:
    c:\documents and settings\users\username\desktop
    vista\7:
    c:\users\username\desktop

    and these are merely default locations. They can be moved by the user to anywhere they wish, even other hard drives.

    2. The filelist control is an activex control and if you use it instead of the standard listbox, it has to be included with the compiled program, making the distributable larger. This is especially apparent with the common dialog control. the control is a couple of megabytes in size, and about 20 lines of code can reproduce it.

    3. Using the controls through api can give you more functionality than the control itself gives you through vb. Richedit comes to mind. It has link functionality that isn't used in the richtextbox control.

    4. The code is actually smaller. If you strip down that code posted above, it's actually a declaration and a call and that's about it. None of the constants are required.
    Code:
    Cnt = SendMessage(List1.hwnd, &H18d, 7, ByVal "c:\*.*")
    besides the single sendmessage declaration, that's all that is required to load the c:\ directory into the listbox, and it's much, MUCH faster than the dir$ command in a loop.
    Lord Orwell, I appreciate your opinion. However, we could also write the same solution in Assembly language with about 300 lines of code. I am sure it would run even faster than the API you suggest.

    I still lilke writing code in VB6 using 6 or 7 lines, even if I lose a few nanoseconds.
    Doctor Ed

  17. #17
    New Member
    Join Date
    Jun 2009
    Posts
    1

    Re: add all files from a folder to a listbox?

    You can use FileListBox instead. Using DriveListBox and DirListBox is much easier.
    I'll share my code to you and you can try it.
    Names of my controls: DriveListBox = DriveList, DirListBox = DirList, FileListBox = FileList

    Code:
    Private Sub Form_Load()
    FileList.Pattern = "*.mp3"    'if you would like to show only mp3 files
    End Sub
    Code:
    Private Sub DriveList_Change()
        On Error GoTo drvError
            DirList.Path = DriveList.Drive
        Exit Sub
    drvError:
        MsgBox "Drive not available.", vbCritical
    
    End Sub
    Code:
    Private Sub DirList_Change()
    FileList.Path = DirList.Path
    End Sub

    Check out website design company.

  18. #18
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: add all files from a folder to a listbox?

    don't get me wrong. Personally, i only hit api if i need it for something that's going to take a load of code to replace. I really don't see anything i posted above being an opinion though. You asked for legitimate reasons and i gave some.
    How exactly do you intend to get the location of a user's folder WITHOUT api though? I'd love to see that code.
    And personally i have written a recursive search in assembly, and it wasn't that large. about 20 lines. It didn't use a listbox though. It was in dos.
    And much, MUCH faster does not equal a few nanoseconds. We're talking visible differences in speed. The refresh of the listbox alone makes the difference. the file access probably isn't any faster. It's the event triggering for every single item you add to the listbox. Of course you could add more lines to your code to counteract that, but then again your code will no longer be 6 or 7 lines. It's the problem with controls in general. Reading and writing to them is intrinsically slow, plus the event-triggering overhead. Just based on experience, i would expect a directory of signifigant size to have a speed difference of at least a magnitude of 100 times. ten milliseconds as compared to a second, in other words.

    Of course, that's only if speed matters. Otherwise just use the filelist control and don't use any code at all.

    Also (and this is a nitpick), it wasn't i who suggested it.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

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