|
-
Dec 13th, 2023, 10:05 AM
#1
Thread Starter
Addicted Member
VB.NET Search Multiple Files for String, Count Duplicates, Return Filename & Qty
Hi All.. I am using some old VBA code that I am trying to convert over to .NET but having a hard time doing what I need. In this case, I am searching a single folder with hundreds of files for a string. I need to find how many times that string is found in the file, return the filename with the quantity found. Here is what I have started with, but this fails after returning the first filename and entering it to the listbox. I have not gotten as far yet as getting the total counts and filename returned. Any guidance on this would be greatly appreciated.
Code:
Dim fpath = Directory.GetCurrentDirectory
Dim filesPath = fpath & "\prg"
Private Sub btnSearchFiles_Click(sender As Object, e As EventArgs) Handles btnSearchFiles.Click
Dim thePart = $" {TextBox1.Text} " 'match whole word
Dim di As New DirectoryInfo(filesPath)
For Each fi As FileInfo In di.GetFiles()
If File.ReadAllText(fi.FullName).Contains(thePart) Then
ListBox1.Items.Add(fi.Name)
MsgBox(fi.Name)
Dim lines As New HashSet(Of String)()
Using sr As StreamReader = New StreamReader(fi.Name)
Do While sr.Peek() >= 0
lines.Add(sr.ReadLine())
ListBox1.Items.Add(lines)
Loop
End Using
End If
Next
End Sub
Trying more things today with no luck... Going crazy here. I hope someone can help me with this.
Code:
Dim fpath = Directory.GetCurrentDirectory
Dim filesPath = fpath
Private Sub btnSearchFiles_Click(sender As Object, e As EventArgs) Handles btnSearchFiles.Click
Dim thePart = $" {TextBox1.Text} " 'match whole word
Dim di As New DirectoryInfo(filesPath)
Dim strText As String = SearchFile(di.FullName, thePart)
If strText <> String.Empty Then
ListBox2.Items.Add(strText)
End If
End Sub
Public Shared Function SearchFile(ByVal filesPath As String, ByVal thePart As String) As String
Dim sr As StreamReader = New StreamReader(filesPath)
Dim strLine As String = String.Empty
Try
Do While sr.Peek() >= 0
strLine = String.Empty
strLine = sr.ReadLine
If strLine.Contains(thePart) Then
sr.Close()
Exit Do
End If
Loop
Return strLine
Catch ex As Exception
Return String.Empty
End Try
End Function
Last edited by mikeg71; Dec 14th, 2023 at 10:23 AM.
-
Dec 15th, 2023, 11:06 AM
#2
Re: VB.NET Search Multiple Files for String, Count Duplicates, Return Filename & Qty
try reading in the entire file into a string, then split the string on the value you are looking for. The number of occurrences would then be the number of elements in the returned array minus 1. It may not be very efficient memory wise, but it will tell you if the value is there and how many times it appears.
kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Dec 15th, 2023, 12:35 PM
#3
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.
-
Dec 15th, 2023, 02:48 PM
#4
Thread Starter
Addicted Member
Re: VB.NET Search Multiple Files for String, Count Duplicates, Return Filename & Qty
@ChrisE Thank you for your help with this, it does 99% of what I need. This is great! So here is what I have now after using your posted code and works well. But how do I get a full/exact match? This is searching for part values, so I will have many that are similar, like: CR232-099G or CR232-098G etc. So if I enter a search for CR232-08F, it finds everything that includes CR232.
**Update** I am looking at this more and I see that it might be all I need. I just want to make sure it is not finding partial matches.
Code:
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Dim fpath = Directory.GetCurrentDirectory
Dim filesPath = fpath
Dim thePart As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
thePart = $"{TextBox1.Text}" 'match whole word
Dim startFolder As String = fpath & "\prg"
Dim fileList As IEnumerable(Of FileInfo) = GetFiles(startFolder)
Dim searchTerm As New Regex(thePart)
Dim queryMatchingFiles = From afile In fileList
Where afile.Extension = ".dp"
Let fileText = File.ReadAllText(afile.FullName)
Let matches = searchTerm.Matches(fileText)
Where (matches.Count > 0)
Select Name = afile.Name,
Matches = From match As Match In matches
Group By match.Value Into g = Group
Select New With {Value, g.Count}
Try
Dim folder As String = fpath & "\parts\"
Dim filename As String = System.IO.Path.Combine(folder, thePart & ".jpg")
PictureBox1.Image = Image.FromFile(filename)
Catch ex As Exception
'Leave image blank
End Try
For Each fileMatches In queryMatchingFiles
Dim s = fileMatches.Name
ListBox2.Items.Add(s)
For Each match In fileMatches.Matches
ListBox2.Items.Add(match.Value + ": " + match.Count.ToString)
Next
ListBox2.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
End Class
Last edited by mikeg71; Dec 15th, 2023 at 03:43 PM.
-
Dec 15th, 2023, 02:49 PM
#5
Thread Starter
Addicted Member
Re: VB.NET Search Multiple Files for String, Count Duplicates, Return Filename & Qty
Thanks @kebo I was just getting this read in like you suggested and did work with the split to find my value.
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
|