1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| Dim FilePath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Teste")
Dim action As Boolean = False
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Not String.IsNullOrEmpty(TextBox1.Text) Then
RichTextBox1.Clear()
Dim files1 As List(Of résult) = SearchText(FilePath, TextBox1.Text)
If files1.Any Then
files1.ForEach(AddressOf Résultats)
Else
RichTextBox1.AppendText(String.Format("Oups, Aucun texte à était trouver : {0}", TextBox1.Text))
End If
Else
MsgBox("Veuillez entrer un texte pour rechercher !", MsgBoxStyle.Exclamation, "Attention")
End If
End Sub
Private Function SearchText(FileNames As String, Mot As String) As List(Of résult)
Dim data As New List(Of résult)
Dim Pos As New List(Of Integer)
action = False
Dim files As String() = Directory.GetFiles(FilePath, "*.*", SearchOption.AllDirectories)
If (files.Length < 1) Then Throw New FileNotFoundException("Attention, il y a aucun fichier dans le dossier : " & FileNames)
For Each fn As String In files
Dim txt As String() = IO.File.ReadAllLines(fn)
If (txt.Length < 1) Then Throw New FileLoadException("Attention, Il n'y a rien dans le fichier !")
For i = 0 To txt.Count - 1
Dim Caractéres As Char() = txt(i).ToCharArray
If (From b In txt(i).ToCharArray Where b = Mot Select b).Count > 0 Then
Pos.Add(i + 1)
action = True
End If
Next
If action Then
For i = 0 To Pos.Count - 1
data.Add(New résult(Path.GetFileName(fn), Mot, Pos.Count, Pos.Item(i)))
Next
End If
Pos.Clear()
Next
Return data
End Function
Private Sub Résultats(ByVal p As résult)
RichTextBox1.AppendText(String.Format("Le fichier : {0} - Le mot à rechercher : {1} ({2} Résultats) {3}", p.Name, p.Pattern, p.Value, Environment.NewLine))
For i = 0 To p.Value - 1 Step 1
RichTextBox1.AppendText(String.Format("Ligne {0} : {1} {2}", p.Offset, p.Pattern, Environment.NewLine))
Next
End Sub
End Class
Public Class résult
Public Property Name As String
Public Property Pattern As String
Public Property Value As Integer
Public Property Offset As Integer
Public Sub New(ByVal _Name As String, ByVal _Pattern As String, ByVal _Value As Integer, ByVal _Offset As Integer)
Me.Name = _Name
Me.Pattern = _Pattern
Me.Value = _Value
Me.Offset = _Offset
End Sub |