I have written and application to search through a text file and find the following data and pull out the 5th varible and place in a text box.

P_SPEED
0 10 1100 0 .01 23 0

The next issue I am having to input, into a text box, a integer and replace the 5th variable in this data. There are several instances of this variable in the file and all will required to be replaced with the new value. After the file will need to be saved.

Any suggestion as to searching for this data and replacing it?

Thanks in advance for any help/direction....

Please can you give me the code if you have got it done....

I am new with vb.net and vb coding in general. So, it took me a while visiting several vb.net forums to help me through this development. In my ingnorance, I thought I could read through the text file find the data I needed and write it at that instance and be done. Rather what has to be done is read each line replace each line in a temp file, while replacing the data I need to replace. Once I have gone through the entire the text file, and replaced the data I needed, I then write the entire temp file to the directory. In this case I overwrote the file I pulled the original data from. For those who are software developers, this code may be very rough as it is my first attemp of creating an application.

Imports System.IO
Module WriteDateFile
    Sub Main()
        '- Location of the Text file
        Dim fileName As String = WattChangeForm.Files.SelectedItem
        '- Set the line counter
        Dim lineNumber As Integer = 0

        '- Open the text file/stream
        Dim sr As StreamReader = New StreamReader(fileName)

        '- Initialize name variables
        Dim sequenzs As String = ""
        Dim pspeed As String = ""
        Dim speed As String = ""
        Dim temp As String = ""
        Dim textline As String = ""
[U]        Dim strFull As String = ""
        Dim strOriginal As String = ""
        Dim strReplace As String = ""
        Dim fline[/U]

        '- Cycle thru the text file 1 line at a time pulling
        '- substrings into the variables initialized above
        Do
            textline = sr.ReadLine()
            temp = temp + textline + vbCrLf
            '- If it goes past the last line of the file,
            '- it drops out of the loop
            If textline <> Nothing Then
                Try
                    sequenzs = textline.Substring(0, 8).Trim()
                    If sequenzs = "SEQUENZs" Then
                        Do
                            textline = sr.ReadLine()
                            temp = temp + textline + vbCrLf
                            If textline = "P_SPEED" Then
                                pspeed = textline.Substring(0, 7).Trim()
                                If pspeed = "P_SPEED" Then
                                    textline = sr.ReadLine()
 [U]                                   fline = Split(textline, " ")
                                    'replace the current from the input current text box
                                    'write it to the file
                                    strFull = textline
                                    strOriginal = fline(5)
                                    strReplace = WattChangeForm.ChangeCurrentbox.Text
                                    strFull = strFull.Replace(strOriginal, strReplace)
                                    textline = strFull
                                    temp = temp + textline + vbCrLf[/U]
                                End If
                            End If
                        Loop Until textline = Nothing
                    End If
                Catch ex As Exception
                    'WattChangeForm.RichTextBox1.Text = "Error nothing to read"
                End Try
            End If
        Loop Until textline = Nothing
        '- Close the file/stream
        sr.Close()

        'after building the temp file with the new values, overwrite the 
        '.lso with the temp file data
        Dim SW As StreamWriter = New StreamWriter(fileName)
        SW.Write(temp)
        SW.Close()
        WattChangeForm.ChangeCurrentbox.Text = ""
        WattChangeForm.RichTextBox1.Text = ""
        Call ReadDataFile.Main()
    End Sub
End Module

The data below is a section of the data I had to go through in the text file. Going down through data I could not use the first 'P_SPEED' data string I came to, rather I had to find the second occurrance and every occurrance after to change the data string that followed. That is why I used a search for the 'SEQUENZs' first and then 'P_SPEED'.

#DataDir=.\
#1=.\VTX\SAMPLE.VTX
#2=.\FTX\SAMPLE.FTX
#3=.\STX\SAMPLE.STX
#4=.\CDS\SAMPLE.CDS
#5=.\PAR\Sample.par
#VERSION = v2.33.3, 08.08.2006 rd
#P_SPEED = W0
#OBJECTIV = 254
#KOORDORIENT = 0
P_SPEED
#[W0]
0 500 100 4000 .01 18 0
#<PARENT_DEF>
M
-52.189 16.33 0
SEQUENZs
50 0 0
P_GALVO
#<VOLATILE>
#[galvo1]
.5 .5 .4 0 .2 0
P_SPEED
0 10 1100 0 .01 25.3 0
C
-52.241 16.692 0 -52.089 17.357 0 -51.886 17.661 0
P_SPEED
0 10 1000 0 .01 25.3 0
L
-44.563 25.588 0
P_SPEED
0 10 1100 0 .01 25.3 0

I know this inexperience talking as I expect it to become more like a job in the future, but this experience was like playing my first computer based game and I am hooked and ready to tackle the next application....

I'm fully aware that this thread is quite old now (I found it on google), but I wanted to post something here:


Here is my own search and replace function(s) for strings.
Just read in a textfile and read every line as a string (and modify that string, then replace the line in that text file with the new string), until the end of file, using these two functions together to do the replacements.

' My own equivalent of VB.NET's inbuilt "instr" function. Because I can
    Function InString(ByRef sMas As String, ByRef sDum As String) As Integer
        For i As Integer = 1 To sMas.Length Step 1
            If Mid(sMas, i, sDum.Length) = sDum Then
                Return i
            End If
        Next
        Return 0
    End Function

    Function SearchAndReplace(ByRef sMas As String, ByRef sDum As String, ByRef sRep As String) As String
        Do Until InString(sMas, sDum) = 0 Or InString(sRep, sDum) > 0
            For i As Integer = 1 To sMas.Length Step 1
                If Mid(sMas.ToLower, i, sDum.ToLower.Length) = sDum.ToLower Then
                    sMas = (Mid(sMas, 1, i - 1) + sRep + Mid(sMas, i + sDum.Length, sMas.Length - i + 1 + sDum.Length))
                    Exit For
                End If
            Next
        Loop
        Return sMas
    End Function

It's only flaw is when the searchitem to replace is also contained in the actual replacement:
e.g.

Call System.Console.WriteLine(SearchAndReplace("redundant box is redundant", "redundant", "non-volatile redundant, hairy, and by all rights awesome"))

Will still return "redundant box is redundant", but what I would ideally want it to return is "non-volatile redundant, hairy, and by all rights awesome box is non-volatile redundant, hairy, and by all rights awesome".

It's a flaw that I would like to fix, but I haven't come up with a solution yet (which means no good search-and-replace in my text editor yet).

' Replaces (with sRep) only the first instance of sDum in the string sMas
    Function searchReplace(ByRef sMas As String, _
                           ByRef sDum As String, _
                           ByRef sRep As String) As String
        Try : sMas = Mid(sMas, 1, InStr(sMas, sDum) - 1) _
               + sRep _
               + Mid(sMas, InStr(sMas, sDum) + sDum.Length _
                  , sMas.Length - InStr(sMas, sDum) + 1 + sDum.Length)
        Catch ex As Exception : End Try : Return sMas
    End Function

    ' Replaces (with sRep) all instances of sDum in the string sMas
    Function allSearchReplace(ByRef sMas As String, _
                              ByRef sDum As String, _
                              ByRef sRep As String) As String
        Dim sTemp As String = "" : Do Until InStr(sMas, sDum) = 0
            Try : sTemp += Mid(sMas, 1, InStr(sMas, sDum) - 1) + sRep _
                : sMas = Mid(sMas, InStr(sMas, sDum) + sDum.Length _
                   , sMas.Length - InStr(sMas, sDum) + 1 + sDum.Length)
            Catch ex As Exception : End Try : Loop : Return sTemp + sMas
    End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.