Results 1 to 2 of 2

Thread: Raed Text file and make changes to its value

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    243

    Raed Text file and make changes to its value

    I have a text file to read. The text file contain two parts. I need to find matching line in this two parts and place the matching lines in new file. In that new file i need to change the value in () from firs part and value in[] in second part. So far i can do all this things. This is a code.

    Code:
    Imports System.IO
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim File() As String = IO.File.ReadAllLines("C:\wires.txt")
            Using sw As New IO.StreamWriter("C:\wirematches.txt")
    
                For x As Integer = 0 To File.Length - 1
                    Dim s As String = Me.GetLineMatch(File(x), x, File)
                    If s <> String.Empty Then
                        sw.WriteLine(s)
                    End If
                Next
            End Using
    
            Dim j As Long = 0
            Dim myReader As StreamReader = New StreamReader("C:\wirematches.txt")
            Dim line As String
            Do
                j += 1
                line = myReader.ReadLine()
    
    
            Loop Until line Is Nothing
    
            myReader.Close()
            myReader = Nothing
            TextBox3.Text = CStr(j)
    
        End Sub
        Private Function GetLineMatch(ByVal LineToMatch As String, ByVal LineNumber As Integer, ByVal FileContents() As String) As String
            Dim value As String = System.Text.RegularExpressions.Regex.Match(LineToMatch, "\([\d \.]+\)").Value.Replace("("c, "["c).Replace(")"c, "]"c)
            If value = String.Empty Then Return String.Empty
            For x As Integer = 0 To FileContents.Length - 1
    
                If FileContents(x).Contains(value) Then
                    Return String.Format("{0}      |   {2}    ", LineToMatch, LineNumber.ToString(), FileContents(x), x.ToString())
    
                End If
    
            Next
            Return String.Empty
    
        End Function
    
    
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    
            Dim lines() As String = IO.File.ReadAllLines("C:\wirematches.txt")
            For x As Integer = 0 To lines.GetUpperBound(0)
                Dim parts() As String = lines(x).Split(New String() {" | "}, StringSplitOptions.None)
                parts(0) = Regex.Replace(parts(0), "\(([0-9]|\s|\.)+\)", "(T" & (x + 1).ToString.PadRight(12) & ")")
                parts(1) = Regex.Replace(parts(1), "\[([0-9]|\s|\.)+\]", "[T" & (x + 1).ToString.PadRight(12) & "]")
                lines(x) = parts(0) & " | " & parts(1)
            Next
            IO.File.WriteAllLines("C:\wirematches.txt", lines)
        End Sub


    Now i want to changes the x and y. For example my new text file is like this

    4.5 28 Red (T1 ) [1 12.86 53.8] 229111 -28275 198452 -1309 | 11.0 28 Red (2 13.10 04.0) [T1 ] 130611 2948 229111 28275


    i need to change the value in red colour and green colour. I want to replace this values

    For example:

    The first line value is 22911. There is no changes in first line but the second line onward i must plus with 2.54 until end of column.

    Let say 22911=X

    So second line onwards the value will be X=X+2.4. Same also for second part . Can any one guid me on how should i point the green and red value and change the values. I attach mu text files here for your referance


    Thank You.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width