2 Replies - 598 Views - Last Post: 13 June 2008 - 06:22 AM

#1 RodgerB   User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Get an array of files in a directory, filtered by extension

Posted 08 June 2008 - 09:36 PM

Description: 1) Add the following statement to the top of your code: Imports System.IO 2) Copy and paste the function into a class. 3) Read the example on how to call the function.Get a list of files in a directory depicted by a string array of extensions.
''' 
''' Gets a list of files in a directory, by extension
''' 
''' The path of the directory to search in
''' The string array of extensions to search with
''' (must be formatted like this (without the quotes): '*.txt' (txt is an example extension)
''' An array of FileInfo, which stores information of each and every file filtered
''' 
Public Function GetFilesByExtensions(ByVal strPath As String, ByVal colExtensions() As String) As FileInfo()
    ' The collection we will be using to store the file information in
    Dim tmpCollection As New Collection

    ' Loop through all the extensions and files, and add them to the collection.
    For Each strExtension As String In colExtensions
        For Each aFile As FileInfo In New DirectoryInfo(strPath).GetFiles(strExtension)
            tmpCollection.Add(aFile)
        Next
    Next

    ' Variables to convert the collection to type FileInfo (more convenient to the programmer)
    Dim tmpFiles(tmpCollection.Count - 1) As FileInfo, i As Integer = 0

    ' Loop through the collection and convert it into FileInfo
    For Each aFile As FileInfo In tmpCollection
        tmpFiles(i) = aFile
        i += 1
    Next

    ' Return the files
    Return tmpFiles
End Function

' Example Usage
Dim extensions() As String = {"*.mp3", "*.wma"}
Dim files() As FileInfo = GetFilesByExtensions("C:UsersPublicMusicSample Music", extensions)

For Each file As FileInfo In files
    MessageBox.Show(file.FullName)
Next


Is This A Good Question/Topic? 0
  • +

Replies To: Get an array of files in a directory, filtered by extension

#2 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Get an array of files in a directory, filtered by extension

Posted 13 June 2008 - 06:15 AM

Function can be shrunk to
    Public Function GetFilesByExtensions(ByVal strPath As String, ByVal colExtensions() As String) As FileInfo()
        ' The collection we will be using to store the file information in
        Dim tmpCollection As New List(Of IO.FileInfo)
        Dim a As System.IO.DirectoryInfo
        ' Loop through all the extensions and files, and add them to the collection.
        For Each strExtension As String In colExtensions
            tmpCollection.AddRange(a.GetFiles(strExtension))
        Next
        ' Return the files
        Return tmpCollection.ToArray
    End Function


This post has been edited by AdamSpeight2008: 11 January 2015 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Get an array of files in a directory, filtered by extension

Posted 13 June 2008 - 06:22 AM

Working shrinkage.

Public Function GetFilesByExtensions(ByVal strPath As String, ByVal colExtensions() As String) As IO.FileInfo()
Dim tmpCollection As New List(Of IO.FileInfo)
Dim a As New System.IO.DirectoryInfo(strPath)
' Loop through all the extensions and add found files to the collection.
For Each strExtension As String In colExtensions
  tmpCollection.AddRange(a.GetFiles(strExtension))
Next
' Return the files
Return tmpCollection.ToArray
End Function

This post has been edited by AdamSpeight2008: 11 January 2015 - 06:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1