Results 1 to 8 of 8

Thread: Adding File Names In A Folder To a Listbox

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    39

    Question Adding File Names In A Folder To a Listbox

    I wanted to add all the names of the files in a folder in my computer to a list box in my vb program, so i used:

    Code:
    Do
              List1.AddItem Dir("G:\foler1\folder2\")
    Loop
    So i expect to get the file names in the path i've chosen ("G:\foler1\folder2\") to be added to List1. But there are 2 problems:

    - the computer doesn't stop looping, so i need a code to tell it stop if there are no more files

    - if the loop works, still the the program only adds the first file name to the listbox and keeps adding the first one in the loop.


    a little help on this please, asap. thank u

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Adding File Names In A Folder To a Listbox

    Try this...

    Code:
    Sub addfilenames()
        Dim fold As Scripting.Folder
        Dim fil As File
        Dim fso As New FileSystemObject
            
        'Replace this with your folder name
        Set fold = fso.GetFolder("S:\aaa")
        
        For Each fil In fold.Files
            
            List1.AddItem fil.Name
           
        Next fil
    End Sub
    Edit: Set reference to Microsoft Scripting Runtime library
    Last edited by Siddharth Rout; Apr 17th, 2008 at 05:00 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Adding File Names In A Folder To a Listbox

    I wouldn't wanna add a dependency to the scripting object for listing files...

    Code:
    Dim strPath As String, strFile As String
    strPath = "G:\foler1\folder2\"
    strFile = Dir$(strPath)
    Do While Len(strFile) > 0
        List1.AddItem strFile
        strFile = Dir$()
    Loop
    I think sometimes it might return ".." as a "go back to previous directory" so you might also need to add:

    Code:
    Dim strPath As String, strFile As String
    strPath = "G:\foler1\folder2\"
    strFile = Dir$(strPath)
    Do While Len(strFile) > 0
        If strFile <> ".." Then List1.AddItem strFile
        strFile = Dir$()
    Loop
    Edit: There's also the FileListBox control in the toolbar...you can just set its path to whatever folder you want to list files from.

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Adding File Names In A Folder To a Listbox

    I wouldn't wanna add a dependency to the scripting object for listing files...
    just curious.... why is that?

    The reason is that I don't see any harm in using scripting object for listing files....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    39

    Re: Adding File Names In A Folder To a Listbox

    Quote Originally Posted by koolsid
    Try this...

    Code:
    Sub addfilenames()
        Dim fold As Scripting.Folder
        Dim fil As File
        Dim fso As New FileSystemObject
            
        'Replace this with your folder name
        Set fold = fso.GetFolder("S:\aaa")
        
        For Each fil In fold.Files
            
            List1.AddItem fil.Name
           
        Next fil
    End Sub
    Edit: Set reference to Microsoft Scripting Runtime library

    I tried that and it gives me error msgs saying:

    Compile error: User-defined type not defined
    and the problem seams to be the "As Scripting.Folder", "As File",.... in the dim statements.

    when i get rid of the "As File" and "As Scripting.Folder", "As New FileSystemObject" another error msg comes up saying "object required" for this part of the code:
    Set fold = fso.GetFolder("G:\folder2/folder1/")


    But anyways i think the FileListBox works for me, do you know what the property in the FileListBox that contains the file name is called? (e.g File1.text?? or file1. ? )

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Adding File Names In A Folder To a Listbox

    That's because as I have mentioned in my post at the bottom... you need to set reference..to Microsoft Scripting Runtime library

    Go to the "Project" menu, and select "References". You will be presented with a long list of available references, just scroll down to Microsoft Scripting Runtime library and select it

    also

    Set fold = fso.GetFolder("G:\folder2/folder1/")


    should be

    Set fold = fso.GetFolder("G:\folder2\folder1")

    I have tested it and it works...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Adding File Names In A Folder To a Listbox

    Quote Originally Posted by koolsid
    just curious.... why is that?

    The reason is that I don't see any harm in using scripting object for listing files....
    If he was already using FSO for something else in his project then I would agree. But I just don't see the point in add 148KB to the project just to list files when there is a just as easy to way to do it using the built-in Dir$() function. Or the FileListBox.

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Adding File Names In A Folder To a Listbox

    I agree with DigiRev.
    However, no need to test for "." and ".." as both of these are directories
    when you use Dir$(sPath & "\"), that will return a normal file only (but not hidden file).
    Code:
       Dim sFName As String
    
       '-- noted that the path must ending with "\"
       sFName = Dir$("G:\foler1\folder2\")
       While Len(sFName)
          List1.AddItem sFName
          sFName = Dir$()
       Wend
    If using FSO, there are two ways Early or Late binding:

    1. Late Binding (no need to set reference)
    Code:
       Dim fso As Object
       Dim fo As Object
       Dim fi As Object
       
       Set fso = CreateObject("Scripting.FileSystemObject")
       '-- the path below may be ended with "\" or not
       Set fo = fso.GetFolder("G:\foler1\folder2")
       For Each fi In fo.Files
          List1.AddItem fi.Name
       Next
       Set fi = Nothing
       Set fo = Nothing
       Set fso = Nothing
    2. Early Binding : "Microsoft Scripting Runtime" needs to be referenced.
    Code:
       Dim fso As Scripting.FileSystemObject
       Dim fo As Scripting.Folder
       Dim fi As Scripting.File
       
       Set fso = New Scripting.FileSystemObject
       Set fo = fso.GetFolder("G:\foler1\folder2")
       For Each fi In fo.Files
          List1.AddItem fi.Name
       Next
       Set fi = Nothing
       Set fo = Nothing
       Set fso = Nothing
    To matching line-by-line with Late binding, I didn't use
    Code:
    Dim fso As New Scripting.FileSystemObject
    It seems to be using Dir$() method is much simpler and faster, it takes less than half the time of the other methods.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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