Link to home
Start Free TrialLog in
Avatar of msout
msout

asked on

Format txt file as delimeted

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>")
        Next
                    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.


Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

What is the size of Line? (meaing size can you post the line value you have problems:start value, and before the remove line, will be nice)
Can you post your sample txt file?
Avatar of msout
msout

ASKER

This is my txt file.

1                      Methylene Chloride              3:32.80       3:31.10           54460           2.913
        [4.646]             ---
2                      trans-1,2-dichloroethene        4:13.90           ---             ---             ---             ---             ---     Area too low
3                      cis-1,2-dichloroethene          5:04.50       5:02.30           68409           2.684         [3.992]             ---
4                      Chloroform                      5:19.60           ---             ---             ---             ---             ---     Area too low
5                      1,2-dichloroethane              6:03.40       5:54.90            1417             ---       [0.03233]             ---
6                      Benzene                         6:52.70           ---             ---             ---             ---             ---     Area too low
7                      1,2-dichloropropane             7:47.30           ---             ---             ---             ---             ---     Area too low
8                      trichloroethene                 8:05.50           ---             ---             ---             ---             ---     Area too low
9                      Toluene                        10:43.20      10:33.50            3803          0.1787        [0.2277]             ---
10                      tetrachloroethene              12:34.00           ---             ---             ---             ---             ---     Not found with current search parameters
11                      chlorobenzene                  13:44.90           ---             ---             ---             ---             ---     Not found with current search parameters
12                      ethylbenzene                   14:22.00      14:20.70           34045          0.9847         [1.431]             ---
13                      m-xylene, p-xylene             14:42.30           ---             ---             ---             ---             ---     Not found with current search parameters
14                      styrene                        15:22.60           ---             ---             ---             ---             ---     Not found with current search parameters
15                      o-xylene                       15:35.70           ---             ---             ---             ---             ---     Not found with current search parameters
16                      Isopropylbenzene               16:42.00           ---             ---             ---             ---             ---     Area too low
17                      1,4-dichlorobenzene            18:54.80      19:10.90           46016           1.780         [2.673]             ---
18                      1,2-dichlorobenzene            19:25.30      19:10.90           46016           2.757         [4.174]             ---

This is what I see on the screen:
Line size is - 140 Methylene Chloride | 2.913 [4.646] ---|
Line size is - 157 trans-1,2-dichloroethene | --- --- Area too low|
Line size is - 140 cis-1,2-dichloroethene | 2.684 [3.992] ---|
Line size is - 157 Chloroform | --- --- Area too low|
Line size is - 140 1,2-dichloroethane | --- [0.03233] ---|
Line size is - 157 Benzene | --- --- Area too low|
Line size is - 157 1,2-dichloropropane | --- --- Area too low|
Line size is - 157 trichloroethene | --- --- Area too low|
Line size is - 140 Toluene |0.1787 [0.2277] ---|
Line size is - 186 tetrachloroethene | ---h current search parameters|
Line size is - 186 chlorobenzene | ---h current search parameters|
Line size is - 141 ethylbenzene | 0.9847 [1.431] ---|
Line size is - 186 m-xylene, p-xylene | ---h current search parameters|
Line size is - 186 styrene | ---h current search parameters|
Line size is - 186 o-xylene | ---h current search parameters|
Line size is - 158 Isopropylbenzene | --- --- Area too low|
Line size is - 141 1,4-dichlorobenzene | 1.780 [2.673] ---|
Line size is - 141 1,2-dichlorobenzene | 2.757 [4.174] ---|

This is what I need to have in the result file:

Methylene Chloride       | 2.913      
trans-1,2-dichloroethene |    ---    
cis-1,2-dichloroethene   | 2.684    
Chloroform               |    ---    
1,2-dichloroethane       |    ---  
Benzene                  |    ---
1,2-dichloropropane      |    ---
trichloroethene          |    ---
Toluene                  | 0.1787    
tetrachloroethene        |    ---    
chlorobenzene            |    ---    
ethylbenzene             |  0.9847    
m-xylene, p-xylene       |   ---    
styrene                  |    ---    
o-xylene                 |    ---    
Isopropylbenzene         |    ---    
1,4-dichlorobenzene      |  1.780    
1,2-dichlorobenzene      |  2.757    

Please, help.
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msout

ASKER

Thank you for the great help!
Glad to help, thanks for the points.