
Originally Posted by
kebo
I think the second overload in my first post will return files that match the criteria in all sub folders.
Yes it does and either of your suggestions should be used over mine unless there is a need for multiple conditions
BTW the reason for my suggest was that multiple conditions could be done i.e. check base file name and base file name extension
Code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = Enumerable.Range(0, 25).Select(Function(i) (Chr(Asc("A") + i))).ToList
ComboBox2.DataSource = New List(Of String) From {".ACCDB", ".BMP", ".SQL"}
End Sub
Private Sub Searcher(ByVal FolderName As String)
Dim Dirs As IEnumerable(Of String) = Nothing
Try
Dim files() As String
Dirs = IO.Directory.EnumerateDirectories(FolderName)
files = IO.Directory.EnumerateFiles(FolderName) _
.Where(Function(CurrentFile As String)
Dim BaseName As String = IO.Path.GetFileName(CurrentFile).ToUpper
'
' Multiple conditions
'
If BaseName.StartsWith(ComboBox1.Text) AndAlso IO.Path.GetExtension(BaseName).ToUpper = ComboBox2.Text Then
Return True
Else
Return False
End If
End Function).ToArray
ListBox1.Items.AddRange(files)
Catch ex As Exception
'Just Skip the denied access dirs
End Try
If Dirs IsNot Nothing Then
For Each Dir As String In Dirs
Searcher(Dir)
Next
Else
' Access denied thrown in Catch above
End If
End Sub
Private Sub cmdExecute_Click(sender As Object, e As EventArgs) Handles cmdExecute.Click
If Not String.IsNullOrWhiteSpace(txtFolderName.Text) Then
If IO.Directory.Exists(txtFolderName.Text) Then
Try
cmdExecute.Enabled = False
Application.DoEvents()
ListBox1.Items.Clear()
Searcher(txtFolderName.Text)
Finally
cmdExecute.Enabled = True
End Try
Else
MessageBox.Show("Folder does not exists.")
End If
Else
MessageBox.Show("Please enter a folder name")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Items = My.Computer.FileSystem.GetFiles(txtFolderName.Text, FileIO.SearchOption.SearchAllSubDirectories, {ComboBox1.Text & "*.*"})
Console.WriteLine(Items.Count)
End Sub
End Class