Hi all,

Please see attached for my query.

Hope someone can help me. Thanks in advance.


-renzlo

Read the first line (verify type 002 or length) and get A into a var. Read the next lines, incrementing a counter, until you get to a line with type 002 or length of header line (don't increment your counter for this line). compare your counter to the A value you've read.
Reset the counter and set A again from the line you've just read and repeat until EOF.

hi adam_k,

thanks for the logic, can you give me some sample?

many thanks.

Hi Everyone,

Can anyone help me with this, adam provided the logic but I can't get it right. Can someone provide me a sample? I just needed this one.

Sincerely yours,

renzlo

Here's my entire code now but it doesn't match, what should be my fix to this problem?

Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim openFile As New OpenFileDialog
        openFile.Filter = "Text File|*.txt"
        openFile.ShowDialog()
        TextBox1.Text = openFile.FileName
        Dim law As New StreamReader(TextBox1.Text)
        Dim comp1 As Integer = 0
        Dim comp2 As Integer = 0
        While Not law.EndOfStream
            Dim basa As String = law.ReadLine()
            If Not basa.Substring(0, 3) = "002" Then
                comp2 += 1
            Else
                comp1 = basa.Substring(12, 5)
                MessageBox.Show(comp1.ToString)
                MessageBox.Show(comp2.ToString)
                comp2 = 0
            End If
        End While
    End Sub
End Class

Attach also the input file.

anyone guys? sorry for being desperate, I really need this.

I'll try to help, though you have to provide some defined.details, one thing at a time.

I believe that you've messed the order here:

comp1 = basa.Substring(12, 5)
                MessageBox.Show(comp1.ToString)
                MessageBox.Show(comp2.ToString)
                comp2 = 0

as you are setting comp1 for the new header and then compare it to the number of lines read already (for the previous header).
Change it to:

MessageBox.Show(comp1.ToString)
                MessageBox.Show(comp2.ToString)
                comp1 = basa.Substring(12, 5)
comp2 = 0

and give it a go.

I'll try to help, though you have to provide some defined.details, one thing at a time.

Hi codeorder,

Here's the scenario:

I have a text file with a contents like this:

00249038201500005                        091                   
003490382015160090720000001The double Vag Initiative           
003490382015160090700000001Cream de la Creme                   
003490382015160090710000001Latina Pink                         
003490382015160090690000001Fortune Coochies                    
003490382015160098740000001DVD gratuit                         
00244408638400002                        091                   
003444086384160098610000001Pack 4 DVD Killergram + 4 DVD Diablo
003444086384160098740000001DVD gratuit                         
00244462573400001                        091                   
003444625734160098740000001DVD gratuit                         
00244471842500003                        091                   
003444718425160075530000001Pack 5 DVD Pierre Moro              
003444718425160098610000001Pack 4 DVD Killergram + 4 DVD Diablo
003444718425160098740000001DVD gratuit                         
00249283960000002                        091                   
003492839600160091270000001My Breast Friend                    
003492839600160098740000001DVD gratuit

I want to compare/validate if the value of the line with "002" line.substring(12, 5) which equal to "00005" against to the total count of lines with "003" which is as you can see it is next to the line with "002" and this will loop until EOF.

I have posted my current code above for your reference.

Thanks a lot.

I believe that you've messed the order here:

comp1 = basa.Substring(12, 5)
                MessageBox.Show(comp1.ToString)
                MessageBox.Show(comp2.ToString)
                comp2 = 0

as you are setting comp1 for the new header and then compare it to the number of lines read already (for the previous header).
Change it to:

MessageBox.Show(comp1.ToString)
                MessageBox.Show(comp2.ToString)
                comp1 = basa.Substring(12, 5)
comp2 = 0

and give it a go.

I'll give this a try. Thanks adam.

See if this helps.

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'MsgBox("File is valid = " & validateFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\test.txt"))'// FOR.TESTING.
        With New OpenFileDialog
            .Title = "select a cool file"
            .Filter = "Text File|*.txt"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then '// check if ok.clicked.
                MsgBox("File is valid = " & validateFile(.FileName))
            End If
        End With
    End Sub

    Private arFileLines(), sToLookFOr, sTotal As String, iGoodLine, iBadLine As Integer
    Private Function validateFile(ByVal selCoolFile As String) As Boolean
        arFileLines = IO.File.ReadAllLines(selCoolFile) '// read file into array.
        With arFileLines(0) '// with first line.
            sToLookFOr = .Substring(0, 3) : sTotal = .Substring(12, 5) '// read line and set the values.
            iGoodLine = 1 : iBadLine = 0 '// reset counters; iGoodLine=1 Not 0, since it read the first line.
        End With
        For i As Integer = 1 To arFileLines.Length - 1 '// loop thru all file lines, starting w/the second line.
            With arFileLines(i)
                If Not .StartsWith(sToLookFOr) Then iBadLine += 1 Else iGoodLine += 1 '// add points as needed.
            End With
        Next
        'MsgBox(iGoodLine & "." & iBadLine)
        If CInt(sTotal) = iGoodLine Then Return True Else Return False '// validate total from line 1 against all found good lines and return as needed.
    End Function
End Class

...and thanks for the defined.details.:)

Just overlooked the post you made and am wondering.

Do you want to check lines that start w/"002", get the total from end of line, and count "the following" lines until it validates against the total from end of line? Then go to next line w/"002" and validate again?

Do you want to check lines that start w/"002", get the total from end of line, and count "the following" lines until it validates against the total from end of line? Then go to next line w/"002" and validate again?

Yes, this is exactly what I wanted. Thanks codeorder.

This should take care of the issue.

Private arFileLines(), sToLookFOr As String
    Private Function isFileValid(ByVal selCoolFile As String) As Boolean
        arFileLines = IO.File.ReadAllLines(selCoolFile) '// read file into array.
        sToLookFOr = "002"
        For i As Integer = 0 To arFileLines.Length - 1 '// loop thru all file lines, starting w/the second line.
            If arFileLines(i).StartsWith(sToLookFOr) Then '// check if current line startsWith your string to look for.
                '// get total of items"CInt(...)" following the line and loop thru the next lines until total.
                For i2 As Integer = i + 1 To i + CInt(arFileLines(i).Substring(12, 5))
                    If Not i2 > arFileLines.Length - 1 Then
                        If arFileLines(i2).StartsWith(sToLookFOr) Then Return False '// if not done w/the total of lines Then Return False.
                    Else
                        Return False '// if last line is missing.
                    End If
                Next
            End If
        Next
        Return True '// since it did not Return False, it can only be a valid File.
    End Function
commented: Very helpful man. +3

This should take care of the issue.

Private arFileLines(), sToLookFOr As String
    Private Function isFileValid(ByVal selCoolFile As String) As Boolean
        arFileLines = IO.File.ReadAllLines(selCoolFile) '// read file into array.
        sToLookFOr = "002"
        For i As Integer = 0 To arFileLines.Length - 1 '// loop thru all file lines, starting w/the second line.
            If arFileLines(i).StartsWith(sToLookFOr) Then '// check if current line startsWith your string to look for.
                '// get total of items"CInt(...)" following the line and loop thru the next lines until total.
                For i2 As Integer = i + 1 To i + CInt(arFileLines(i).Substring(12, 5))
                    If Not i2 > arFileLines.Length - 1 Then
                        If arFileLines(i2).StartsWith(sToLookFOr) Then Return False '// if not done w/the total of lines Then Return False.
                    Else
                        Return False '// if last line is missing.
                    End If
                Next
            End If
        Next
        Return True '// since it did not Return False, it can only be a valid File.
    End Function

Thanks for the code, codeorder. I really appreciate it a lot. One more thing, I want to know which line that mismatched, how can specify that?

Replace line.10 with this.

If arFileLines(i2).StartsWith(sToLookFOr) Then 
msgbox("this line is invalid: " & i2.tostring)
 Return False '// if not done w/the total of lines Then Return False.
End If

+= Glad I could help.:)

thanks codeorder, this is now fixed.

Be a part of the DaniWeb community

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