Hi guys,

I am a newbe in vb.net. Started doing a little project recently.
What I want to do is imagine I have a tab delimited file. then that file will have lines no longer than 800 chars. What I need to do is split the file in two parts.
The criteria is a list of string values . In every row I might have one or none of the strings from the list at the same position say strating from 96 character and count 8 cgaracters after in every single row.
So if I have a match then that row with all the formatting should be written in a new file say b.txt. If not the roe be written in diferent file say a.txt.
I have put string values in an array and am looping through original file which is based under my C drive.
However the code doesn`t seem to compare the values at all.
Please help me I am obviously missing something, just not sure what.
I checked if all variables return the right result - they do.
The copmarisson however doesn`t work.
I get an empty row in the b.txt file.
Here is my code:

Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form


    Private Sub Read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Click

        Dim fs As New FileStream("C:\Test.txt", FileMode.Open, FileAccess.Read)


        RichTextBox1.Text = ""

        Dim d As New StreamReader(fs)

        d.BaseStream.Seek(0, SeekOrigin.Begin)

        While d.Peek() > -1
            Dim s As String = d.ReadLine()
            If s.Length > 800 Then
                s = s.Remove(800)

            End If
            RichTextBox1.Text &= s & Environment.NewLine
       'here I declare a single row from the original file with lenght 800 char
            Dim Line As String = Mid(RichTextBox1.Text,1,800)
'the string value from each row 
            Dim RowValue As String = Mid(Line, 96, 9)
'array containing my criteria
            Dim StrArray() As String = {"Value1", "Value2", "Value3", "Value4"}
            For Each Str As String In StrArray
'comparison array against string value in every row
                If String.Compare(Str, RowValue) = 0 Then
                    RichTextBox2.Text &= Line & Environment.NewLine

                End If
            Next



'write to the file b.txt the rows that have string values matching one of the values in the array


            Dim fs2 As New FileStream("C:\b.txt", FileMode.Create, FileAccess.Write)
            Dim ss1 As New StreamWriter(fs2)
            ss1.WriteLine(RichTextBox2.Text)
            ss1.Close()
 
End While
 End Sub

Also not really sure how to tell the program if not match then write the row to file a.txt

Thanks in advance

Recommended Answers

All 2 Replies

I am obviously missing something

Yes, you are.

You take a Dim RowValue As String = Mid(Line, 96, 9) which would be a nine character long string.

Then you compare it with strings Dim StrArray() As String = {"Value1", "Value2", "Value3", "Value4"} each with the length of six characters. That's why you'll never get any matches.

I slightly modified your code

Dim fs As New FileStream("C:\Test.txt", FileMode.Open, FileAccess.Read)
RichTextBox1.Text = ""
RichTextBox2.Text = ""

Dim d As New StreamReader(fs)

d.BaseStream.Seek(0, SeekOrigin.Begin)

Dim StrArray() As String = {"Value1", "Value2", "Value3", "Value4"}
Dim s As String
Dim Str As String
Const LOOKUP_POSITION As Integer = 96 ' Change this value if needed (>=0)

While d.Peek() > -1
  s = d.ReadLine()
  If s.Length > 800 Then
    s = s.Substring(0, 800)
  End If
  RichTextBox1.AppendText(s & Environment.NewLine)
  For Each Str In StrArray
    If s.IndexOf(Str) = LOOKUP_POSITION Then
      RichTextBox2.AppendText(s & Environment.NewLine)
    End If
  Next
  Dim fs2 As New FileStream("C:\b.txt", FileMode.Create, FileAccess.Write)
  Dim ss1 As New StreamWriter(fs2)
  ss1.WriteLine(RichTextBox2.Text)
  ss1.Close()
End While

Now your "code strings" can have a variable length, only starting position is checked.

The comparison is case sensitive. If you want to make a case insensitive comparison, change the line If s.IndexOf(Str) = LOOKUP_POSITION Then to If s.IndexOf(Str, StringComparison.InvariantCultureIgnoreCase) = LOOKUP_POSITION Then

Teme 64 ,

Thanks so much for your help. Saw your response a bit late :P. The mail was gone into the junk folder.
I will try the code you gave of course as I intend to do something very similar to this.

Thanks again for you torough explanation.
Have a good day

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.