|
-
Dec 15th, 2023, 12:35 PM
#4
Re: VB.NET Search Multiple Files for String, Count Duplicates, Return Filename & Qty
this will search in a Directory , you can add multiple words to search
you need to add these Ref.
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Text
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim startFolder As String = "E:\Berichte"
Dim fileList As IEnumerable(Of FileInfo) = GetFiles(startFolder)
'find these words
Dim searchTerm As New Regex("Paris|London|Tommy")
Dim queryMatchingFiles = From afile In fileList
Where afile.Extension = ".txt"
Let fileText = File.ReadAllText(afile.FullName)
Let matches = searchTerm.Matches(fileText)
Where (matches.Count > 0)
Select Name = afile.FullName,
Matches = From match As Match In matches
Group By match.Value Into g = Group
Select New With {Value, g.Count}
ListBox1.Items.Add("The term " & searchTerm.ToString & " was found in:")
For Each fileMatches In queryMatchingFiles
Dim s = fileMatches.Name
ListBox1.Items.Add(s)
For Each match In fileMatches.Matches
ListBox1.Items.Add("found: " + match.Value + " : " + match.Count.ToString)
Next
ListBox1.Items.Add("------------------------------")
Next
End Sub
Shared Function GetFiles(root As String) As IEnumerable(Of FileInfo)
Return From file In My.Computer.FileSystem.GetFiles(
root, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
Select New FileInfo(file)
End Function
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|