Hello,

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!

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

Attachments
[H
VERSION="4.0 Alpha"
WW="6.0.17"
HP="1"
IN="0"
GX="1"
BFS="1"
GY="0"
GXY="0"
UP="0"
FM="1"
FW="200"
ZS="20"
HS="0"
OP="1"
MAT="WEEKE"
DN="STANDARD"
INCH="0"
VIEW="NOMIRROR"
ANZ="1"
BES="0"
ENT="0"
_BSX=600.000000
_BSY=576.000000
_BSZ=19.000000
_FNX=0.000000
_FNY=0.000000
_RNX=0.000000
_RNY=0.000000
_RNZ=0.000000
_RX=600.000000
_RY=576.000000

[001
l="600"
KM="Lengte in X"
b="576"
KM="Breedte in Y"
d="19"
KM="Dikte in Z"
Arug="22"
KM="Afstand van de rug"
Drug="10"
KM="Dikte van de rug"
Makelaar="1"
KM="1 = makelaar 0 = geen"

<100 \WerkStck\
LA="l"
BR="b"
DI="d"
FNX="0"
FNY="0"
AX="0"
AY="0"

<139 \Komponente\
IN="poels/Deuvel_Hor.mpr"
XA="0.0"
YA="0.0"
ZA="0.0"
EM="0"
VA="l l"
VA="b b"
VA="d d"
VA="Arug Arug"
KAT="Komponentenmakro"
MNM="Deuvel_Hor"
ORI=""
KO="00"

<139 \Komponente\
IN="poels/Groef_rug.mpr"
XA="0.0"
YA="0.0"
ZA="0.0"
EM="0"
VA="l l"
VA="b b"
VA="d d"
VA="Arug Arug"
VA="Drug Drug"
KAT="Komponentenmakro"
MNM="Groef_rug"
ORI=""
KO="00"

<139 \Komponente\
IN="poels/boring_makelaar.mpr"
XA="l/2"
YA="0.0"
ZA="0.0"
EM="0"
VA="l l"
VA="b b"
VA="d d"
KAT="Komponentenmakro"
MNM="boring_makelaar"
ORI=""
KO="00"
??="Makelaar = 1"

<117 \NCStop\
KM="0"
VL="1"
XA="l+800"
F_="STANDARD"
YA="0"
_Y="0"
_X="1"
XV="0"
YV="0"
WT="0"
ZR="0"
KAT="0"
MNM="Stop"
ORI=""
KO="00"

<139 \Komponente\
IN="poels/Voet_kast.mpr"
XA="120"
YA="120"
ZA="0.0"
EM="0"
VA="l l"
VA="b b"
VA="d d"
KAT="Komponentenmakro"
MNM="Voet_kast"
ORI=""
KO="00"

<139 \Komponente\
IN="poels/Voet_kast.mpr"
XA="120"
YA="b-120"
ZA="0.0"
EM="0"
VA="l l"
VA="b b"
VA="d d"
KAT="Komponentenmakro"
MNM="Voet_kast"
ORI=""
KO="00"

<139 \Komponente\
IN="poels/Voet_kast.mpr"
XA="l-120"
YA="b-120"
ZA="0.0"
EM="0"
VA="l l"
VA="b b"
VA="d d"
KAT="Komponentenmakro"
MNM="Voet_kast"
ORI=""
KO="00"

<139 \Komponente\
IN="poels/Voet_kast.mpr"
XA="l-120"
YA="120"
ZA="0.0"
EM="0"
VA="l l"
VA="b b"
VA="d d"
KAT="Komponentenmakro"
MNM="Voet_kast"
ORI=""
KO="00"

<139 \Komponente\
IN="poels/Voet_kast.mpr"
XA="l/2"
YA="120"
ZA="0.0"
EM="0"
VA="l l"
VA="b b"
VA="d d"
KAT="Komponentenmakro"
MNM="Voet_kast"
ORI=""
KO="00"
??="Makelaar = 1 "
!

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

l="600"

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

Some text l="600" some other text

hello,

The value can go from 1 to 6 digits

example:

0.02
1
5.55
500
500.61
1000.61

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)
            Else
              Print #writeFile, AktTxt
            End If
          Loop
        Else
          Print #writeFile, AktTxt
        End If
      Loop

      Close #readFile
      Close #writeFile
    End If
  Next
  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
        Next

        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.

Edited 2 Years Ago by cgeier

This question has already been answered. Start a new discussion instead.