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