Solved

Populate list box with multiple file types

Posted on 2008-10-03
5
243 Views
Last Modified: 2013-11-27
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
Comment
Question by:cheshirefire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 22649113
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
 

Author Comment

by:cheshirefire
ID: 22649589
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
 
LVL 19

Accepted Solution

by:
alb66 earned 150 total points
ID: 22649799
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
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 150 total points
ID: 22649912
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
 

Author Closing Comment

by:cheshirefire
ID: 31502691
Many thanks to you all, problem solved
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application vs Workbook.applicaiton 3 49
Is AWS.RDS considered a part of "Big Data"? 4 158
Is IHttpActionResult a promise pattern? 2 81
Mobile apps - web/native or hybrid? 1 120
This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question