Hi,
I am Populating a combobox from a directory but im wanting to filter some file names from showing in a specific Combobox. Im a newb at this VB stuff and could use the help! Here's the code im using to pull the filenames from my C:\ drive but i need to filter out certain names with the same .ext from that folder to use in the different Comboboxes.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each s As String In System.IO.Directory.GetFiles("C:\backuprouter\")
            ComboBox1A.Items.Add(IO.Path.GetFileNameWithoutExtension(s))
            ComboBox1B.Items.Add(IO.Path.GetFileNameWithoutExtension(s))
            ComboBox2A.Items.Add(IO.Path.GetFileNameWithoutExtension(s))
            ComboBox2B.Items.Add(IO.Path.GetFileNameWithoutExtension(s))
        Next
    End Sub

Thanks for the help

You could use RegEx, but that may be overkill. I think GetFiles supports glob matching like GetFiles("C:\dir", "*.exe"), but you don't want to call GetFiles multiple times. So maybe a simple If statement would suffice:

If IO.Path.GetExtension(s) = ".exe" Then
    MyOtherBox.Items.Add(IO.Path.GetFileNameWithoutExtension(s))
Else
    WithoutThisTypeBox.Items.Add(IO.Path.GetFileNameWithoutExtension(s))
End If

My VB.Net is rusty, but I think that would do it. There is also s.EndsWith(".exe").

Im actually not looking to single them out by the extensions. I need them to be filtered based on file names as such
Combobox1A shows files begining with CE
Combobox1B shows files Becinging with BE

Right now my boxes show every file within the folder and I need to be able to sort them through the different comboboxes according to the beging characters of the file name.

You could try the following using regular expressions

Dim Files() As String = System.IO.Directory.GetFiles("D:\Utils")
Dim fltr As New ArrayList
Dim opt As RegexOptions = RegexOptions.IgnoreCase

'Add all filenames starting with CE

For Each file As String In Files
    Dim basename As String = My.Computer.FileSystem.GetFileInfo(file).Name
    If Regex.IsMatch(basename, "^CE.*", opt) Then
        fltr.Add(basename)
    End If
Next

ComboBox1.DataSource = fltr

fltr.Clear()

'Add all filenames starting with BE

For Each file As String In Files
    Dim basename As String = My.Computer.FileSystem.GetFileInfo(file).Name
    If Regex.IsMatch(basename, "^BE.*", opt) Then
        fltr.Add(basename)
    End If
Next

ComboBox2.DataSource = fltr

In regular expressions, the caret (^) anchors the pattern to the start of the string. The ".*" matches any string, so "^CE.*" matches any file that starts with CE.

Edited 1 Year Ago by Reverend Jim

This question has already been answered. Start a new discussion instead.