Results 1 to 5 of 5

Thread: [RESOLVED] [2008] Getting a list of files with certain file types

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Resolved [RESOLVED] [2008] Getting a list of files with certain file types

    Hi!
    How do I get a list of files with a certain extension in a directory and return that list in a combobox?
    e.g
    Say I want to find all the text files in the "C:\" directory and return their names in a combobox, how would I go about doing it?

    Any help is much appreciated.

    Louix.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] Getting a list of files with certain file types

    Code:
            Dim di As New System.IO.DirectoryInfo("C:\")
            Dim fi() As System.IO.FileInfo
            fi = di.GetFiles("*.txt")
            For ct As Integer = 0 To fi.Length - 1
                Debug.WriteLine(fi(ct).FullName)
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2008] Getting a list of files with certain file types

    Use the GetFiles function.
    Code:
    Option Explicit On
    Option Strict On
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim xlFiles() As String
            xlFiles = System.IO.Directory.GetFiles("C:\Users\UserName\Documents", "*.xls", IO.SearchOption.TopDirectoryOnly)
            ComboBox1.Items.AddRange(xlFiles)
        End Sub
    
    End Class
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Getting a list of files with certain file types

    If you want only the file names, rather than the full paths, displayed in the ComboBox you'd modify what's already been posted:
    vb.net Code:
    1. myComboBox.DisplayMember = "Name"
    2. myComboBox.ValueMember = "FullName"
    3. myComboBox.DataSource = (New IO.DirectoryInfo("folder path here")).GetFiles("*.txt")
    By binding an array of FileInfo objects to the control you have access to the file name only as well as the full path. Setting the DisplayMember to "Name" will display just the file names in the control, while setting the ValueMember to "FullName" will return the full path of the selected file from the control's SelectedValue property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Thumbs up Problem Now Solved! Thanks a lot guys! :D

    Thanks sooooooooooo much guys you solved my problem and it now works exactly how I wanted it to!
    Again, thanks so much!!!!!!
    Louix.

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