We help IT Professionals succeed at work.
Get Started

Format txt file as delimeted

msout asked
Last Modified: 2012-05-11
I need to format text file as delimeted and delete some fields. this is my code (vb.net, vs2008)
 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim filename As String = "C:\temp\WaterPurge1.txt"
        Dim Lines As New List(Of String)(IO.File.ReadAllLines(filename))
        For Each Line In Lines
            Dim count As Integer = Line.Count
             Line.Remove(0, 1)
            Line = Line.Insert(49, "|")
            Line.Insert(101, "|")
            Line = Line.Insert(110, "|")
            Line = Line.Remove(110, count - 135)
            Line = Line.Insert(137, "|")                      
            Line = Line.Remove(50, 53)
            'Line = Line.Remove(141, 46)  this does not work
            Line = Line.Substring(23)
            Response.Write(Line & "<br>")
                    IO.File.WriteAllLines("C:\temp\WaterPurge2.txt", Lines.ToArray())
On the screen I can see all these modifications. Only I have problem to get rid of the fields at the end of each line.  'Line = Line.Remove(141, 46)  this does not work
Line.Remove(110, count - 135) - this works but every line has different count and some of the fields are not deleted completely - couple words is left.
And finally I cannot write the file
IO.File.WriteAllLines("C:\temp\WaterPurge2.txt", Lines.ToArray())   -
this line of code writes the file in initial format without any modifications.

I wanted to use TextFieldParser Object but cannot do any modifications of the line there.

Please, help.

Watch Question
Top Expert 2015
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE