I am trying to create a converter to read a very long text file looking for a line that contains a certain string. When it finds this string it should write the line it found to a new file along with other lines before and after.
My application works but is only repeating the first found line in all the groups of lines it creates.
For example, a group in the old file looks like this:
[PARTICLE930]
INDEX = 930 ;º§¶óÅä076_¼ÕµîÀÌÆåÆ®_¿À¸¥
ABSAXIS=0
PARTICLE=.\Chef\60LV_WHITE\B\BF\076\hand_eff_r.spt
I want to extract just the line beginning with PARTICLE=.\ and create the new lines around it automatically. Because the group numbers are sequential and don’t have gaps, I don’t need to extract the index number, I can create it on the fly.
My code so far:
Dim source As String = "Particle.ini"
Dim dest As String = "CParticle.ini"
Dim path As String = Application.StartupPath
Dim max As Integer = 1233
Dim l_pnum As String = "[PARTICLE{0}]"
Dim l_index As String = "INDEX = {1}"
Dim l_absaxis As String = "ABSAXIS=0"
Dim l_particle As String = "{2}"
If System.IO.File.Exists(path & "\" & source) Then
Dim objReader As New System.IO.StreamReader(path & "\" & source)
Dim objWriter As New System.IO.StreamWriter(path & "\" & dest, True)
objWriter.WriteLine("[PARTICLE_INFO]")
objWriter.WriteLine("MAXPARTICLE=" & max.ToString)
Dim loop_num As Integer = 1
Do While objReader.Peek() <> -1
Dim line_read As String = objReader.ReadLine()
If line_read.Contains("PARTICLE=.\") Then
l_pnum = l_pnum.Replace("{0}", loop_num.ToString)
l_index = l_index.Replace("{1}", loop_num.ToString)
l_particle = l_particle.Replace("{2}", line_read)
objWriter.WriteLine(l_pnum)
objWriter.WriteLine(l_index)
objWriter.WriteLine(l_absaxis)
objWriter.WriteLine(l_particle)
loop_num = loop_num + 1
End If
Loop
Else
MessageBox.Show("File does not exist" & vbNewLine & path & "\" & source)
End If
As I said above, it works but it’s just repeating index 1 over and over for as many times as there are groups to convert.
Effectively all I want to do is remove all the white gaps, and the foreign characters from the file while retaining it’s structure.
I’m not sure what I’ve done wrong here. Anyone able to help?
It looks like you are wiping out your formatting on your first replace, therefore it never writes the next record.
You setup your Strings as such:
Then when you write your first record you replace your master with your results meaning you no longer have the formatting to write out the next record.
Either create temporary variables to hold your results or use the result without assigning it to your initial string.
i.e.