2

I have the below code that looks for the value displayed in a combobox then populates a listbox with the selected file names that relate the extension selected (maybe the code will make more sense!)

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles    ComboBox1.SelectedIndexChanged

    Dim lyxfle As New IO.DirectoryInfo(sPath)
    Dim diar1 As IO.FileInfo() = lyxfle.GetFiles()
    Dim MyExt As String = Nothing

    Dim MyVariable As String

    Dim sFile As String

    MyVariable = ComboBox1.Text

    If MyVariable = "Forms" Then MyExt = "*.pff"
    If MyVariable = "Reports" Then MyExt = "*.mdb"
    If MyVariable = "Spreadsheets" Then MyExt = "*.xlsm"
    ListBox2.Items.Clear()

    sFile = Dir$(sPath & MyExt)

    Do While CBool(Len(sFile))
        ListBox2.Items.Add(System.IO.Path.GetFileNameWithoutExtension(sFile))

        sFile = Dir$()
    Loop
End Sub

The following line is what i'm struggling with

If MyVariable = "Spreadsheets" Then MyExt = "*.xlsm"

I basically need it to also look at the extensions .xls & .xlsx and include all files within the list

Both Path and sPath are declared as Private Strings at the top of the class

Any advice

Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
elmonko
  • 665
  • 2
  • 12
  • 29
  • 1
    I would probably ditch the Dir$ command and do this: http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters – WozzeC Oct 16 '13 at 12:04
  • Thanks for the link but being very new to vb (this week) I'm struggling to how to implement the change to the Dir$ command – elmonko Oct 16 '13 at 12:39

1 Answers1

1

I'll try to keep this simple and use what you have then. This code will get all the files from the folder. Then it will match it to your MyExt selection. As you can see I have added the excel file extensions with a comma between them (can be any special character really). Then all I have to do is see if MyExt contains the FileExtension and add it to the listbox:

    Dim MyExt As String = Nothing

    Dim MyVariable As String

    Dim sFile As String

    MyVariable = ComboBox1.Text

    If MyVariable = "Forms" Then MyExt = "*.pff"
    If MyVariable = "Reports" Then MyExt = "*.mdb"
    If MyVariable = "Spreadsheets" Then MyExt = ".xlsm,.xls"

    Dim lyxfle As New IO.DirectoryInfo(sPath)
    Dim diar1 As IO.FileInfo() = lyxfle.GetFiles()

    For Each file As IO.FileInfo In diar1

        If MyExt.Contains(file.Extension) Then

            ListBox2.Items.Add(IO.Path.GetFileNameWithoutExtension(file.Name))
        End If

    Next
WozzeC
  • 2,630
  • 1
  • 13
  • 12