I am using a custom class to find files by name, recursively in a given directory. I want to make it smarter by letting it accept regular expressions. How can I go about doing this?
Below is my class and the calling code:
Class:
Imports System.IO
Public Class FileSearcher
Private _Matches As New ArrayList
Private _FileFilter As String
Private _Recursive As Boolean
Public ReadOnly Property Matches() As ArrayList
Get
Return _Matches
End Get
End Property
Public Property FileFilter() As String
Get
Return _FileFilter
End Get
Set(ByVal Value As String)
_FileFilter = Value
End Set
End Property
Public Sub New(ByVal fileFilter As String)
Me.FileFilter = fileFilter
End Sub
Public Sub Search(ByVal startingPath As String, _
ByVal recursive As Boolean)
Matches.Clear()
Me._Recursive = recursive
SearchDirectory(New DirectoryInfo(startingPath))
End Sub
Private Sub SearchDirectory(ByVal dir As DirectoryInfo)
Get the files in this directory.
Dim FileItem As FileInfo
For Each FileItem In dir.GetFiles(Me.FileFilter)
If the file matches, add it to the collection.
_Matches.Add(FileItem)
Next
Process the subdirectories.
If Me._Recursive Then
Dim DirItem As DirectoryInfo
For Each DirItem In dir.GetDirectories()
Try
This is the recursive call.
SearchDirectory(DirItem)
Catch Err As UnauthorizedAccessException
Error thrown if you dont have security permissions
to access directory - ignore it.
End Try
Next
End If
End Sub
End Class
Calling Code:
Dim Searcher As New FileSearcher("s*.dgn")
Searcher.Search(MyRootFolder, True)
For Each cFile In Searcher.Matches
Me.lstCandidates.Items.Add(cFile.Name)
Next
Any suggestions????????
Below is my class and the calling code:
Class:
Imports System.IO
Public Class FileSearcher
Private _Matches As New ArrayList
Private _FileFilter As String
Private _Recursive As Boolean
Public ReadOnly Property Matches() As ArrayList
Get
Return _Matches
End Get
End Property
Public Property FileFilter() As String
Get
Return _FileFilter
End Get
Set(ByVal Value As String)
_FileFilter = Value
End Set
End Property
Public Sub New(ByVal fileFilter As String)
Me.FileFilter = fileFilter
End Sub
Public Sub Search(ByVal startingPath As String, _
ByVal recursive As Boolean)
Matches.Clear()
Me._Recursive = recursive
SearchDirectory(New DirectoryInfo(startingPath))
End Sub
Private Sub SearchDirectory(ByVal dir As DirectoryInfo)
Get the files in this directory.
Dim FileItem As FileInfo
For Each FileItem In dir.GetFiles(Me.FileFilter)
If the file matches, add it to the collection.
_Matches.Add(FileItem)
Next
Process the subdirectories.
If Me._Recursive Then
Dim DirItem As DirectoryInfo
For Each DirItem In dir.GetDirectories()
Try
This is the recursive call.
SearchDirectory(DirItem)
Catch Err As UnauthorizedAccessException
Error thrown if you dont have security permissions
to access directory - ignore it.
End Try
Next
End If
End Sub
End Class
Calling Code:
Dim Searcher As New FileSearcher("s*.dgn")
Searcher.Search(MyRootFolder, True)
For Each cFile In Searcher.Matches
Me.lstCandidates.Items.Add(cFile.Name)
Next
Any suggestions????????