0

What I'm trying to accomplish is reading a text file and selecting certain lines to modify by filling in text from a second form. Here is an example the code I'm currently using. What's happening is I'm looking for a line that starts with 718 and then somewhere after that line there will be a line that starts with 720. I need both lines to fill in the second form. The only way I can think of is to just keep adding 1 to the line until it reaches the line I need. I'm still new to this and I'm sure there's an easier way to do this maybe using Try or While but I'm not sure how. Appreciate any help.

Dim lines() As String = File.ReadAllLines(tempsave)
For i As Integer = 0 To lines.Length - 1
If lines(i).StartsWith("718") Then
  If lines(i + 1).StartsWith("720") Then
       Dim array() As String = lines(i).Split("*"c, "~"c)
       Dim array2() As String = lines(i + 1).Split("*"c, "~"c)
       FormFill.TextBox1.Text = array(3)
       FormFill.TextBox2.Text = array(9)
       FormFill.ShowDialog()
       lines(i) = lines(i).Replace(array(3), FormFill.TextBox1.Text)
       lines(i + 1) = lines(i + 1).Replace(array(9), FormFill.TextBox2.Text)
  Else
       If lines(i + 2).StartsWith("720") Then
           Dim array() As String = lines(i).Split("*"c, "~"c)
           Dim array2() As String = lines(i + 2).Split("*"c, "~"c)
           FormFill.TextBox1.Text = array(3)
           FormFill.TextBox2.Text = array(9)
           FormFill.ShowDialog()
              lines(i) = lines(i).Replace(array2(3),FormFill.TextBox1.Text)
              lines(i + 2) = lines(i + 2).Replace(array(9), FormFill.TextBox2.Text)
       End If
   End If
End If
Next

Example Data:

Input:
123*test*test*test~
718*test*test*test~
543*test*test*test~
720*test*test*test~

Output:
123*test*test*test~
718*test*test*newdata~
543*test*test*test~
720*test*test*newdata~
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Nick
  • 155
  • 4
  • 16
  • You know how to use `Split`, so why not split the entire file to get 1. what comes after 718 `s.Split({$"{Environment.NewLine}718"}, StringSplitOptions.RemoveEmptyEntries)`, then on the result of that to get what comes after 720 `s.Split({$"{Environment.NewLine}720"}, StringSplitOptions.RemoveEmptyEntries)`, or something along those lines. – djv Jan 07 '20 at 15:45
  • The file contains multiple lines, it's not just one long string. I also need the lines to stay in the exact position they're in. I just need to modify some of the text on each line. I'll add example data. – Nick Jan 07 '20 at 15:54
  • I accounted for multiple lines, see I split with a newline then 718 for example. This would find them... but if you need to modify the lines in place, then it won't work. Can you show an actual input, and desired output? – djv Jan 07 '20 at 16:01
  • Please see above for input output result. – Nick Jan 07 '20 at 16:31
  • It's not clear what you need. Do you need modify text file itself or just get data from there and populate form? – JohnyL Jan 07 '20 at 16:43
  • So what I'm doing is both. I need to pull the line say:718*test*test~ into a form with a free text box containing that data, modify it and then put it back with the new data. 718*test*newdata~ – Nick Jan 07 '20 at 16:43
  • The second 718 line was not mentioned in your question, yet you've modified its line. So it's unclear what you need to do. – djv Jan 07 '20 at 17:06
  • Sorry, there's multiple 718 lines in the file so I need to modify them one batch at a time. I'll remove the second one. – Nick Jan 07 '20 at 17:45
  • *there's multiple 718 lines in the file so I need to modify them one batch at a time* And how do you suppose to modify ALL of them in ONE text box? How'd you upload them into ONE text box? – JohnyL Jan 07 '20 at 17:57
  • They won't all be modified in one text box. Please look at my example above. You can see that I populate 2 text boxes and then replace the lines with the text from those boxes. My example above works, I'm just looking for a better way to do it. – Nick Jan 07 '20 at 18:08
  • What's the size of the text file? – JohnyL Jan 07 '20 at 18:14
  • Also in your loop you also use next line `arr(i + 1)`, but in your example nothing happens to next line. – JohnyL Jan 07 '20 at 18:17
  • The text file is small, 100-200 lines. I think my example is making it to difficult to understand. I need to find 2 lines. One that starts with 718 and then after that line will be a line that starts with 720. I don't know how far down 720 will be, this is why I'm using (i+1) until I find it. The answer below @intexx actually works, but I need to replace those lines with modified data not just read them. – Nick Jan 07 '20 at 18:23
  • 1
    `My example above works, I'm just looking for a better way to do it.` A better fit for this would have been [Code Review](https://codereview.stackexchange.com/). StackOverflow is targeted at helping people solve very specific problems, not addressing general design issues such as this one. See [How to Ask](https://stackoverflow.com/help/how-to-ask). HTH – InteXX Jan 07 '20 at 21:35
  • Sorry that's my fault, I didn't realize there was a code review section. Thanks for that information. – Nick Jan 08 '20 at 13:05

1 Answers1

1

Here, try this:

Public Sub Lines()
  Dim _
    aNextLines,
    aAllLines As String()

  Dim _
    s718Line,
    s720Line As String

  aAllLines = IO.File.ReadAllLines("D:\Logs\Data.log")

  For i As Integer = 0 To aAllLines.Length - 1
    If aAllLines(i).StartsWith("718") Then
      s718Line = aAllLines(i)
      aNextLines = aAllLines.Skip(i + 1).ToArray
      s720Line = aNextLines.FirstOrDefault(Function(Line) Line.StartsWith("720"))

      ' Process data here

    End If
  Next
End Sub

--UPDATE--

Here's a modified version that both reads and writes:

Public Sub Lines()
  Dim oForm As FormFill

  Dim _
    aNextLines,
    aAllLines As String()

  Dim _
    i718Index,
    i720Index As Integer

  Dim _
    s718Line,
    s720Line As String

  oForm = New FormFill

  aAllLines = IO.File.ReadAllLines(oForm.FilePath)
  s718Line = String.Empty
  s720Line = String.Empty

  For i718Index = 0 To aAllLines.Length - 1
    If aAllLines(i718Index).StartsWith("718") Then
      s718Line = aAllLines(i718Index)
      aNextLines = aAllLines.Skip(i718Index + 1).ToArray

      For i720Index = 0 To aNextLines.Length - 1
        If aNextLines(i720Index).StartsWith("720") Then
          s720Line = aNextLines(i720Index)

          Exit For ' Assumes only one 720 line in the file
        End If
      Next

      Exit For ' Assumes only one 718 line in the file
    End If
  Next

  oForm.TextBox718.Text = s718Line
  oForm.TextBox720.Text = s720Line

  oForm.TextBox718.Tag = i718Index
  oForm.TextBox720.Tag = i720Index
End Sub

Now, in your Save button's Click event handler:

Private Sub SaveButton_Click(Sender As Button, e As EventArgs) Handles SaveButton.Click
  Dim aAllLines As String()

  Dim _
    i718Index,
    i720Index As Integer

  Dim _
    s718Line,
    s720Line As String

  s718Line = Me.TextBox718.Text
  s720Line = Me.TextBox720.Text

  i718Index = Me.TextBox718.Tag
  i720Index = Me.TextBox720.Tag

  aAllLines = IO.File.ReadAllLines(Me.FilePath)

  aAllLines(i718Index) = s718Line
  aAllLines(i720Index) = s720Line

  IO.File.WriteAllLines(Me.FilePath, aAllLines)
End Sub

That should do it.

InteXX
  • 6,135
  • 6
  • 43
  • 80
  • This is perfect for reading the data. Now I just need to know how I can modify the lines? `lines(i) = lines(i) + Form.Textbox1.Text` will work for line 718 but how about the 720 line? – Nick Jan 07 '20 at 19:01
  • @Nick ~ See my edit for some additional ideas. Note that overall the solution is made more difficult—and significantly more brittle—by the fact that a text file is being treated as a database. Do you have any control over that? – InteXX Jan 07 '20 at 21:23
  • @Nick ~ YW, happy to do it. – InteXX Jan 08 '20 at 17:11