MTNCOMP | List View | Table View | myBlog (1762 Entries) |
Imports System.IO
Sub RecursiveSearch()
Dim searcher As New Task(Of IList(Of String))(Function()
Return GetFiles("C:\", "*.txt")
End Function)
searcher.Start()
Console.WriteLine("Searching for .txt files in C:\ and all subdirectories...")
searcher.Wait()
Dim files = searcher.Result
Console.WriteLine(String.Format("{0:N0} .txt files found.", files.Count))
Console.ReadKey()
End Sub
Function GetFiles(directoryPath As String, filter As String) As IList(Of String)
Dim files As New List(Of String)
Dim directories() As String = Nothing
Try
files.AddRange(Directory.GetFiles(directoryPath, filter))
directories = Directory.GetDirectories(directoryPath)
Catch ex As Exception
Console.WriteLine($"Error while processing folder {directoryPath}.")
End Try
If directories IsNot Nothing Then
For Each directory In directories
files.AddRange(GetFiles(directory, filter))
Next
End If
Return files
End Function
© 2024 myBlog™ v1.1 All rights reserved. We count views as reads, so let's not over think it.