-
Mar 22nd, 2018, 10:27 AM
#1
Thread Starter
Banned
Reading Multiple text file from a folder and count specific words
Hi guys,
I would like to know how to Read Multiple text file from a folder and count specific words from those text files ( using a button command )
Example:
Read DM01 ( not only DM01 , Sm05 too ) from all text files in a specific folder and output to a textbox in my program ( and add an exception which will count even if it lowercased or uppercased )
Example of text file: ( if ur wondering the DM01 and SM05 is always on the same line of each text document in the folder )
https://prnt.sc/iuvcya
Need only the count value on the textbox like
Example : 2 ( is the count value output it to textbox )
Please note that the files format cannot be changed to any other format other than .txt)
Last edited by ixes7777; Mar 22nd, 2018 at 10:31 AM.
-
Mar 22nd, 2018, 02:29 PM
#2
Re: Reading Multiple text file from a folder and count specific words
Here is code you can start with then adapt it to meet your needs
Code:
Option Strict On
Option Explicit On
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With TextBox1
.Multiline = True
.ScrollBars = ScrollBars.Both
.Width = 400
.Height = 400
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strFiles() As String = Directory.GetFiles("replace\with\your\folder\path", "*.txt", IO.SearchOption.TopDirectoryOnly)
Dim strLines() As String
Dim sb As New StringBuilder
Dim intDm1 As Integer
Dim intSm5 As Integer
For j = 0 To strFiles.Length - 1
strLines = File.ReadAllLines(strFiles(j))
intDm1 = 0
intSm5 = 0
For j = 0 To strFiles.Length - 1
strLines = File.ReadAllLines(strFiles(j))
intDm1 = 0
intSm5 = 0
For k = 0 To strLines.Length - 1
If strLines(k).Trim.StartsWith("DM01") Then
intDm1 += 1
ElseIf strLines(k).Trim.StartsWith("SM05") Then
intSm5 += 1
End If
Next
sb.AppendLine(" Found DM01 " & intDm1 & " times, SM05 " & intSm5 & " times in the file """ & IO.Path.GetFileName(strFiles(j)) & """")
Next
TextBox1.Text = sb.ToString
End Sub
End Class
-
Mar 22nd, 2018, 09:58 PM
#3
Thread Starter
Banned
Re: Reading Multiple text file from a folder and count specific words
Originally Posted by 4x2y
Here is code you can start with then adapt it to meet your needs
Code:
Option Strict On
Option Explicit On
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With TextBox1
.Multiline = True
.ScrollBars = ScrollBars.Both
.Width = 400
.Height = 400
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strFiles() As String = Directory.GetFiles("replace\with\your\folder\path", "*.txt", IO.SearchOption.TopDirectoryOnly)
Dim strLines() As String
Dim sb As New StringBuilder
Dim intDm1 As Integer
Dim intSm5 As Integer
For j = 0 To strFiles.Length - 1
strLines = File.ReadAllLines(strFiles(j))
intDm1 = 0
intSm5 = 0
For j = 0 To strFiles.Length - 1
strLines = File.ReadAllLines(strFiles(j))
intDm1 = 0
intSm5 = 0
For k = 0 To strLines.Length - 1
If strLines(k).Trim.StartsWith("DM01") Then
intDm1 += 1
ElseIf strLines(k).Trim.StartsWith("SM05") Then
intSm5 += 1
End If
Next
sb.AppendLine(" Found DM01 " & intDm1 & " times, SM05 " & intSm5 & " times in the file """ & IO.Path.GetFileName(strFiles(j)) & """")
Next
TextBox1.Text = sb.ToString
End Sub
End Class
Hi ^-^ thanks alot for the help but it checks every file in "D:\Logs", "*.txt" for that specific words and then calculate how many is there and just provide only the value
Just the value of it nothing more total value
I Think for only value to be there i gotta add
sb.AppendLine(& intDm1)
Like this right ? Then it will only show the value of it.
I was wondering if this would work
Code:
Dim strFiles() As String = Directory.GetFiles("\sharepoint3.stomaldives.org/sites/medicalservice/documents/Planning", "*.txt", IO.SearchOption.TopDirectoryOnly)
Last edited by ixes7777; Mar 23rd, 2018 at 12:44 AM.
-
Mar 23rd, 2018, 12:44 AM
#4
Re: Reading Multiple text file from a folder and count specific words
Strange, I just noticed that my code is buggy due to wrong copy paste, it should be
Code:
Option Strict On
Option Explicit On
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With TextBox1
.Multiline = True
.ScrollBars = ScrollBars.Both
.Width = 400
.Height = 400
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strFiles() As String = Directory.GetFiles("replace\with\your\folder\path", "*.txt", IO.SearchOption.TopDirectoryOnly)
Dim strLines() As String
Dim sb As New StringBuilder
Dim intDm1 As Integer
Dim intSm5 As Integer
For j = 0 To strFiles.Length - 1
strLines = File.ReadAllLines(strFiles(j))
intDm1 = 0
intSm5 = 0
For k = 0 To strLines.Length - 1
If strLines(k).Trim.StartsWith("DM01") Then
intDm1 += 1
ElseIf strLines(k).Trim.StartsWith("SM05") Then
intSm5 += 1
End If
Next
sb.AppendLine(" Found DM01 " & intDm1 & " times, SM05 " & intSm5 & " times in the file """ & IO.Path.GetFileName(strFiles(j)) & """")
Next
TextBox1.Text = sb.ToString
End Sub
End Class
-
Mar 23rd, 2018, 12:59 AM
#5
Thread Starter
Banned
Re: Reading Multiple text file from a folder and count specific words
Would this work ?
Im using shared network drive as a path
Code:
Dim strFiles() As String = Directory.GetFiles("\sharepoint3.stomaldives.org/sites/medicalservice/documents/Planning", "*.txt", IO.SearchOption.TopDirectoryOnly)
-
Mar 23rd, 2018, 01:12 AM
#6
Re: Reading Multiple text file from a folder and count specific words
I don't know, just try!
Does your main problem resolved?
-
Mar 23rd, 2018, 01:35 AM
#7
Thread Starter
Banned
Re: Reading Multiple text file from a folder and count specific words
Originally Posted by 4x2y
Here is code you can start with then adapt it to meet your needs
Code:
Option Strict On
Option Explicit On
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With TextBox1
.Multiline = True
.ScrollBars = ScrollBars.Both
.Width = 400
.Height = 400
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strFiles() As String = Directory.GetFiles("replace\with\your\folder\path", "*.txt", IO.SearchOption.TopDirectoryOnly)
Dim strLines() As String
Dim sb As New StringBuilder
Dim intDm1 As Integer
Dim intSm5 As Integer
For j = 0 To strFiles.Length - 1
strLines = File.ReadAllLines(strFiles(j))
intDm1 = 0
intSm5 = 0
For j = 0 To strFiles.Length - 1
strLines = File.ReadAllLines(strFiles(j))
intDm1 = 0
intSm5 = 0
For k = 0 To strLines.Length - 1
If strLines(k).Trim.StartsWith("DM01") Then
intDm1 += 1
ElseIf strLines(k).Trim.StartsWith("SM05") Then
intSm5 += 1
End If
Next
sb.AppendLine(" Found DM01 " & intDm1 & " times, SM05 " & intSm5 & " times in the file """ & IO.Path.GetFileName(strFiles(j)) & """")
Next
TextBox1.Text = sb.ToString
End Sub
End Class
Originally Posted by 4x2y
I don't know, just try!
Does your main problem resolved?
Yes solved thanks alot bro
-
Mar 23rd, 2018, 02:09 AM
#8
Re: Reading Multiple text file from a folder and count specific words
Originally Posted by ixes7777
Yes solved thanks alot bro
Good, then mark this thread RESOLVED from above Thread tools menu and start new thread if you still have problem with listing files in network drive.
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
|