Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Populate list box with multiple file types

I want to populate a list box from a directory with multiple particular file types, jpg or bmp or dwf only. can anyone suggest some code for vb express 2008.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TextBox1.Text = "c:\test\"
        Dim files() As String = IO.Directory.GetFiles(TextBox1.Text)
        ListBox1.Items.Clear()
        For Each file As String In files
            ListBox1.Items.Add(IO.Path.GetFileName(file))
        Next
    End Sub

Open in new window

0
cheshirefire
Asked:
cheshirefire
  • 2
  • 2
2 Solutions
 
alb66Commented:
You can use the second parameter to specify the file type:

Dim files() As String = IO.Directory.GetFiles(TextBox1.Text, "*.jpg")

See
http://msdn.microsoft.com/en-us/library/ms143316.aspx
0
 
cheshirefireAuthor Commented:
My apologies what I meant was *.jpg AND *.bmp AND *.dwf exclusively.

The directory has several other file types that another program uses i.e. htm's, txt's  so all I want are the above file types.

thanks
0
 
alb66Commented:
It should be:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ListBox1.Items.Clear()
        TextBox1.Text = "c:\test\"
 
        Dim files1() As String = IO.Directory.GetFiles(TextBox1.Text, "*.jpg")
        For Each file As String In files1
            ListBox1.Items.Add(IO.Path.GetFileName(file))
        Next
 
        Dim files2() As String = IO.Directory.GetFiles(TextBox1.Text, "*.bmp")
        For Each file As String In files2
            ListBox1.Items.Add(IO.Path.GetFileName(file))
        Next
 
        Dim files3() As String = IO.Directory.GetFiles(TextBox1.Text, "*.dwf")
        For Each file As String In files3
            ListBox1.Items.Add(IO.Path.GetFileName(file))
        Next
    End Sub

Open in new window

0
 
Bob LearnedCommented:
How about a little optimization:

Example:

Dim fileList As List(Of String) = FileSystemObject.GetFilteredFileList("C:\Windows\Web\Wallpaper", "jpg", "bmp")
Imports System.Collections.Generic
Imports System.IO
 
Public Class FileSystemObject
 
    ''' <summary>
    ''' Get a filtered list of files, based on the file extensions passed.
    ''' </summary>
    ''' <param name="filePath">The full path directory name</param>
    ''' <param name="extensionList">A list of extensions (i.e. jpg, gif, bmp, png)</param>
    ''' <returns>A list of </returns>
    ''' <remarks></remarks>
    Public Shared Function GetFilteredFileList(ByVal filePath As String, ByVal ParamArray extensionList As String()) As List(Of String)
 
        Dim fileList As New List(Of String)
 
        For Each extension As String In extensionList
            fileList.AddRange(Directory.GetFiles(filePath, "*." & extension))
        Next extension
        Return fileList
 
    End Function
 
End Class

Open in new window

0
 
cheshirefireAuthor Commented:
Many thanks to you all, problem solved
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now