How do I replace a string in a text file with another string, including quotes?

For example, I have an xml sting that reads:

<test name="My name is Rick" value="False" />

and I want replace to replace the value="False" to value="True" so that it ends up like this:

<test name="My name is Rick" value="True" /> and it must include the quotes.

This is part of my vbs code

Dim FileName, FileName2, Find, ReplaceWith, FileContents, dFileContents
Find         = WScript.Arguments(0)
ReplaceWith  = WScript.Arguments(1)
FileName     = WScript.Arguments(2)
FileName2    = WScript.Arguments(3)

Wscript.Echo ("Find: " & Find & vbCrLf & "Replacewith: " & Replacewith & vbCrLf & "FileName: " & FileName & vbCrLf & "FileName2: " + FileName2)
FileContents = GetFile(FileName)
dFileContents = Replace(" & FileContents & ", Find, " & ReplaceWith & ", 1, -1, 1)
if dFileContents <> FileContents Then
  WriteFile FileName2, dFileContents
  Wscript.Echo "Searched string Not In the source file"
End If

Recommended Answers

All 7 Replies

Add chr(34) to each side of the string. 34 is the value for double quote.

Add chr(34) to each side of the string. 34 is the value for double quote.

Like this:
replace(chr(34)"My name is Rick" value="False"chr(34), chr(34)"My name is Rick" value="True"chr(34)) ? That does not seem to work.

That's because you didn't add the chr(34) .

myquotedstring = chr$(34) & "My name is Rick" & chr$(34)
myquotedstring = chr$(34) & "My name is Rick" & chr$(34)

Problem is - I have two sets of quotes in my string:

<test name="My name is Rick" value="False" />

I want to search for string that has "My name is Rick" and then replace value="False" to value="True".

Replace(chr$(34)"My name is Rick" value="False"chr$(34), chr$(34)"My name is Rick" value="True"chr$(34))

This does not work. I'm going to simplify my question. Take a look at this code:

dim output, txt
txt=chr(34)<test name="My name is Rick" value="False" />chr(34)

Try copy and pasting the above in notepad, save as vbs and run it, using chr(34) or chr$(34) it does not work. I can make it work if I have NO double quotes in my string, but I have four double quotes and that throws everything off.

you need to think about what vb is parsing, it is processing the " not passing them in and you are not concatenating your strings.

txt = "<test name=" &chr(34) & "My Name is Rick" & chr(34) & " value=" & chr(34) & "False" & chr(34) & "/>"

Some versions of VB (including VBS, I believe) allow you to specify double quotes within a string by simply putting two double-quotes together like so:

dim output, txt
txt = "<test name=""My Name is Rick"" value=""False""/>"
output = Replace(txt, "False", "True")

So, your line 11 above can be corrected like so:

dFileContents = Replace(FileContents, chr(34) & Find & chr(34), chr(34) & ReplaceWith & chr(34), 1, -1, 1)
' or like this...
dFileContents = Replace(FileContents, """" & Find & """", """" & ReplaceWith & """", 1, -1, 1)

Maybe that will at least point you in the right direction? Good luck!

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.