Results 1 to 8 of 8

Thread: Reading Multiple text file from a folder and count specific words

  1. #1

    Thread Starter
    Banned
    Join Date
    Mar 2018
    Location
    GB
    Posts
    12

    Question 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.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  3. #3

    Thread Starter
    Banned
    Join Date
    Mar 2018
    Location
    GB
    Posts
    12

    Red face Re: Reading Multiple text file from a folder and count specific words

    Quote Originally Posted by 4x2y View Post
    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.

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  5. #5

    Thread Starter
    Banned
    Join Date
    Mar 2018
    Location
    GB
    Posts
    12

    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)

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Reading Multiple text file from a folder and count specific words

    I don't know, just try!

    Does your main problem resolved?



  7. #7

    Thread Starter
    Banned
    Join Date
    Mar 2018
    Location
    GB
    Posts
    12

    Re: Reading Multiple text file from a folder and count specific words

    Quote Originally Posted by 4x2y View Post
    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
    Quote Originally Posted by 4x2y View Post
    I don't know, just try!

    Does your main problem resolved?
    Yes solved thanks alot bro

  8. #8
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Reading Multiple text file from a folder and count specific words

    Quote Originally Posted by ixes7777 View Post
    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
  •  



Click Here to Expand Forum to Full Width