I need help i am tired of dealing with this program and trying to get it to work. All i am doing is reading each txt file i find in a direcory and finding certain strings in the text then counting how many i find in each file and then once each file has its separete counts it send it to a sql database. I have most of it done , but i cant seem to get the count to separate each file so my problem now is that it counts the first file but when it counts the next file it adds to the first file and so on through all the files

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Collections
Imports System.Data.SqlClient

Partial Public Class Form1
    Inherits Form
    Public Sub New()
        Dim fileListing As New DirectoryInfo("\\New Folder\")
        Dim ifile As Integer
        Dim strfile As String
        For Each file As FileInfo In fileListing.GetFiles("*.*")
        ifile = 0
        For ifile = ifile To lstBoxFiles.Items.Count - 1

            strfile = "\\New Folder\" + lstBoxFiles.Items(ifile).ToString
            Dim r As New System.IO.StreamReader(strfile)
            While r.Peek <> -1
                Dim filecontents As String = String.Empty
                'add each line to the contents variable
                filecontents = r.ReadLine
                Dim A08count As String
                Dim totalA08minus As Integer
                Dim totalA08 As Integer
                Dim A01count As String
                Dim totalA01minus As Integer
                Dim totalA01 As Integer
                Dim BARcount As String
                Dim totalBAR As Integer
                Dim totalBARminus As Integer
                Dim MSHcount As String
                Dim totalMSH As Integer
                Dim totalMSHminus As Integer

                Dim subdate As String = filecontents
                Dim subdatevalue As String
                If filecontents.Contains(":2009") Then
                    subdate = subdate.Substring(subdate.IndexOf("2009") + 0, 8)
                    subdatevalue = subdate.ToString
                End If

                If filecontents.Contains("|ADT^A08|") Then

                    Dim foundindex As Integer = filecontents.ToString.IndexOf("|ADT^A08|")
                    A08count = filecontents.Substring(filecontents.IndexOf("|ADT^A08|"))
                    totalA08 += 1
                    totalA08minus = totalA08.ToString.Count + 1

                End If
                If filecontents.Contains("|ADT^A01|") Then

                    Dim foundindex As Integer = filecontents.ToString.IndexOf("|ADT^A01|")
                    A01count = filecontents.Substring(filecontents.IndexOf("|ADT^A01|"))
                    totalA01 += 1
                    totalA01minus = totalA01.ToString.Count - 1
                End If
                If filecontents.Contains("|BAR^P01|") Then

                    Dim foundindex As Integer = filecontents.ToString.IndexOf("|BAR^P01|")
                    BARcount = filecontents.Substring(filecontents.IndexOf("|BAR^P01|"))
                    totalBAR += 1
                    totalBARminus = totalBAR.ToString.Count - 1
                End If
                Dim totalfinalA08 As Integer

                ' adds all the data that was read from the streamreader to richtextbox1
                Dim someText As String = filecontents
                Dim strLen As Integer = someText.Length
                Dim finished As Boolean = False

                If r.EndOfStream = True Then

                    Dim typeString As String
                        typeString = ListBox1.Items.ToString()
                    Catch ex As Exception
                        MessageBox.Show("Nothing was found there dummy " + ex.Message + "")
                    End Try

                    Dim SQLStringMSH As String = ""

                    Dim Server As String = ""
                    Dim Username As String = ""
                    Dim Password As String = ""
                    Dim Database As String = ""
                    Dim ConnectionString As String = "Data Source=" + Server + ";"
                    ConnectionString += "User ID=" + Username + ";"
                    ConnectionString += "Password=" + Password + ";"
                    ConnectionString += "Initial Catalog=" + Database
                    Dim SQLConnection As New SqlConnection()
                    Dim cmd As New SqlCommand
                    SQLConnection.ConnectionString = ConnectionString
                    cmd.Connection = SQLConnection

                        cmd.CommandType = CommandType.Text
                        'SQLStringMSH = "INSERT INTO IMPACTstats (MSH, A01, A08, A11, A02, CLI, IMG, UNK, PRE, REG, PUL, BAR,ORM, Date ) VALUES ('" + waytotal.ToString + "', '" + "', '" + foundcountA08.ToString + "', '" + foundcountA11.ToString + "', '" + foundcountA02.ToString + "', '" + foundcountCLI.ToString + "', '" + foundcountIMG.ToString + "', '" + foundcountUNK.ToString + "', '" + foundcountPRE.ToString + "', '" + foundcountREG.ToString + "', '" + foundcountPUL.ToString + "', '" + foundcountBAR.ToString + "', '" + foundcountORM.ToString + "', '" + subdate.ToString + "')"
                        SQLStringMSH = "INSERT INTO IMPACTstats (MSH, A01, A08, BAR, Date ) VALUES ('" + totalMSH.ToString + "', '" + totalA01.ToString + "', '" + totalA08.ToString + "', '" + totalBAR.ToString + "', '" + subdatevalue.ToString + "')"
                        cmd.CommandText = SQLStringMSH
                    Catch Ex As Exception
                        ' Try to close the connection
                        ' If SQLConnection <> Nothing Then
                        '  End If
                        ' Create a (useful) error message
                        Dim ErrorMessage As String = "A error occurred while trying to connect to the server."
                        ErrorMessage += Environment.NewLine
                        ErrorMessage += Environment.NewLine
                        ErrorMessage += Ex.Message
                        ' Show error message (this = the parent Form object)
                        MessageBox.Show(Me, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                        ' Stop here
                    End Try
                End If

            End While

    End Sub

    Private Sub Button1Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1Start.Click

    End Sub

    Private Sub btnUPSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUPSQL.Click

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub

    Private Sub lstBoxFiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstBoxFiles.SelectedIndexChanged

        If lstBoxFiles.SelectedIndex = 0 Then
        End If
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

    End Sub

    Private Sub Button1Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1Exit.Click
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ListBox2_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged

    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

    End Sub
End Class
Re: need help with count loop 80 80

You could change the integer definitions to be set to 0 every time so that it will always set them to 0.

Dim totalA08minus As Integer = 0

I don't necessarily see this being the issue, but from what I can tell, it should be working properly.

As a side note, a suggestion would be to create a procedure to do the looking for your and pass the search string to it so that you don't have almost identical steps occurring in the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.