0

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
Else
  Wscript.Echo "Searched string Not In the source file"
End If
4
Contributors
7
Replies
9
Views
5 Years
Discussion Span
Last Post by BitBlt
0

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.

0
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)
output=replace(txt,"False","True") 
msgbox(output)

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.

Edited by RickL66: n/a

0

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) & "/>"

0

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")
msgbox(output)

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!

Edited by BitBlt: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.