I need a litle help here.

I am creating a program that finds a specific sentence in a word path and replaces it with a given value.

Specific sentence: l="600"

And it has to be changed to : l="300"

This is not to dificult and works fine:

Public Class Form1

Private Property sign As String

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim sign1 As String
    Dim sign2 As String

    sign1 = ("""600""")
    'sign1 = ("""")
    sign2 = ("""300""")

    Dim source As String = "C:\temp\bodem.mpr" 'path from original file
    Dim destination As String = "C:\temp\bodem2.mpr" 'path new file
    Dim oldText As String = "l=" & sign1 'old value
    Dim newText As String = "l=" & sign2 'new value

    My.Computer.FileSystem.WriteAllText(destination, My.Computer.FileSystem.ReadAllText(source).Replace(oldText, newText), False)
    Process.Start("C:\temp\bodem2.mpr") 'opens program in wordpath to control value
End Sub

End Class

The only problem is that the originnal sentence : l="600" does not always have the value 600, but instead always has a different vallue that i dont know. how can i write this down in my code.

Thanks in advance!

Recommended Answers

All 6 Replies

This is an example file. you just need to replace .txt by .mpr

I suggest you use regular expressions. I can give you an example but first I would have to know the range of values that you will be replacing. For example, will the value in quotes always be three digits? Does the string


always appear on its own line or can it occur within a line as in

Some text l="600" some other text


The value can go from 1 to 6 digits



And it wil always appear in its own line.

I did have this kind of code already in vb excell, but in vb.net it doesnt work.

                readFile = FreeFile

readFile = FreeFile
      Open BestandsNaamInput For Input As #readFile

      writeFile = FreeFile
      Open BestandsNaamOutput For Output As #writeFile

      Do Until EOF(readFile)
        Line Input #readFile, AktTxt
        If InStr(AktTxt, BerijkGrensStart) <> 0 Then
          'Schrijf [001 in writeFile
          Print #writeFile, AktTxt
          Do Until AktTxt = ""
            Line Input #readFile, AktTxt
            If InStr(AktTxt, LengtePos) <> 0 Then
              Print #writeFile, LengtePos & Chr(34) & Replace(Lengte, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, BreedtePos) <> 0 Then
              Print #writeFile, BreedtePos & Chr(34) & Replace(Breedte, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, DiktePos) <> 0 Then
              Print #writeFile, DiktePos & Chr(34) & Replace(Dikte, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, OvermaatXPos) <> 0 Then
              Print #writeFile, OvermaatXPos & Chr(34) & Replace(OvermaatX, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, OvermaatYPos) <> 0 Then
              Print #writeFile, OvermaatYPos & Chr(34) & Replace(OvermaatY, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, variabele1Pos) <> 0 Then
              Print #writeFile, variabele1Pos & Chr(34) & Replace(variabele1, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, variabele2Pos) <> 0 Then
              Print #writeFile, variabele2Pos & Chr(34) & Replace(variabele2, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, variabele3Pos) <> 0 Then
              Print #writeFile, variabele3Pos & Chr(34) & Replace(variabele3, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, variabele4Pos) <> 0 Then
              Print #writeFile, variabele4Pos & Chr(34) & Replace(variabele4, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, variabele5Pos) <> 0 Then
              Print #writeFile, variabele5Pos & Chr(34) & Replace(variabele5, ",", ".") & Chr(34)
            ElseIf InStr(AktTxt, variabele6Pos) <> 0 Then
              Print #writeFile, variabele6Pos & Chr(34) & Replace(variabele6, ",", ".") & Chr(34)
              Print #writeFile, AktTxt
            End If
          Print #writeFile, AktTxt
        End If

      Close #readFile
      Close #writeFile
    End If
  MsgBox "Genereren bestanden compleet.", vbInformation

  Exit Sub

Something like the following should work:

        'path from original file
        Dim source As String = "C:\temp\regextest.txt"

        'path new file
        Dim destination As String = "C:\temp\regextest2.txt"

        Dim newText As String = "300" 'new value

        Dim inputText As String = My.Computer.FileSystem.ReadAllText(source)

        'split lines
        Dim inputTextArray As String() = inputText.Split(New String() {"\r\n", "\n", "\r", System.Environment.NewLine}, StringSplitOptions.None)

        'used named capturing groups
        Dim pattern As String = "(?<label>l)=([""])(?<tag>.*)([""])"

        'create a substitution pattern for replace method
        Dim replacePattern As String = "${label}=""" & newText & """"

        Dim outputText As String = String.Empty

        For Each lineData As String In inputTextArray
            Dim result As String = Regex.Replace(lineData, pattern, replacePattern, RegexOptions.IgnoreCase)

            outputText += result & System.Environment.NewLine

        My.Computer.FileSystem.WriteAllText(destination, outputText, False)

        'My.Computer.FileSystem.WriteAllText(destination, My.Computer.FileSystem.ReadAllText(source).Replace(oldText, newText), False)
        Process.Start(destination) 'opens program in wordpath to control value

Adapted from here.

Wow, this works fine.
Now i can finally look further to get my program running.

Be a part of the DaniWeb community

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