How Can I Write Double Quotes to a Text File?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m using the FileSystemObject to create an XML file. However, I need to put double quote marks around some of the items in that file. How do I do that?

— JP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JP. According to your email, you’re trying to write the following line to your XML file:

<? xml version = “1.0” encoding = “UTF-8” ?>

However, you’re getting hung up on the double quotes that surround 1.0 and UTF-8. Double quotes can be a major nuisance when trying to write data to text files (and an XML file, of course, is just a text file); that’s because double quotes are used to, among other things, indicate where strings begin and end. A line of code like this is bound to end in failure:

objFile.WriteLine “<? xml version = “1.0” encoding = “UTF-8″ ?>”

Why? Well, like we said, double quotes indicate the beginning and ending of strings. As far as our script is concerned, your string actually consists of this: “<? xml version = “. In other words, the string consists of everything embedded inside the first set of double quotes. Thus the script reads along, sees “<? xml version = “ and assumes that’s the end of the string. However, you have some additional text following that string: 1.0″ encoding = “UTF-8″ ?>”. That’s not a valid scripting command, and VBScript has no idea what that means. As a result, it simply throws up its hands in despair, and the script fails.

To get around this we need to find a different way to specify that double quotes should be written to the text file. There are a couple different ways to do this, but we’re going to focus on just one (although we’ll briefly show you the other at the end of this column): we’re going to use VBScript’s Chr function to represent double quotes. As you probably know, all the characters you can type on the keyboard are represented by an ASCII value; for example, double quotes have an ASCII value of 34. If we want to insert double quotes into a file we can do this by specifying Chr(34); the Chr function takes the ASCII value – 34 – and converts it to an actual character (in this case double quotes).

Trust us; it’s much easier than it sounds. Here’s a script that writes the desired line to the file C:\Scripts\Test.xml:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.CreateTextFile(“C:\Scripts\test.xml”)

objFile.WriteLine “<? xml version = ” & chr(34) & “1.0” & chr(34) & _ ” encoding = ” & chr(34) & “UTF-8″ & chr(34) & ” ?>”

objFile.Close

We begin by creating an instance of the FileSystemObject and then calling the CreateTextFile method to create the file C:\Scripts\Test.xml. That brings us to the line of code that writes the desired line to the file:

objFile.WriteLine “<? xml version = ” & chr(34) & “1.0” & chr(34) & _
    ” encoding = ” & chr(34) & “UTF-8″ & chr(34) & ” ?>”

Yes, it looks weird, but it’s really not that bad. All we’re doing is calling the WriteLine method, and asking WriteLine to do the following.

First, we need it to compose the string to be written, starting with the characters <? xml version = . (Note the space after the equal sign.) After that, tack on the character with the ASCII value of 34. That happens to be this: . Our string starts out looking like this:

<? xml version = ”

Of course, we’re not done yet. Next, we add the characters 1.0 followed by another set of double quotes. That gives us a string that looks like this:

<? xml version = “1.0”

Can you see where this is all headed? We add the phrase encoding = (again, note the blank space; we need that to ensure that our completed string is spaced properly), followed in rapid-fire succession by double quotes, the characters UTF-8 and another set of double quotes. In other words:

<? xml version = “1.0” encoding = “UTF-8”

Now we just need to add the final chunk of characters: ?>”. With the string fully composed, WriteLine will write the following to the XML file:

<? xml version = “1.0” encoding = “UTF-8” ?>

Make sense? All we did is take the string we want to write and surround it with double quotes:

“<? xml version = “1.0” encoding = “UTF-8″ ?>”

As we know, that’s not valid VBScript syntax, so we then replace each set of embedded quotes with & Chr(34) & (and making sure that all the other characters are surrounded by double quotes). For example, here we’ve replaced the double quotes surrounding 1.0:

“<? xml version = ” & Chr(34) & “1.0” & Chr(34) & ” encoding = “UTF-8″ ?>”

And here we’ve replaced the double quotes surround UTF-8:

“<? xml version = ” & Chr(34) & “1.0” & Chr(34) & ” encoding = ” & Chr(34) & “UTF-8″ & Chr(34) & ” ?>”

To test this construction we can echo the finished product to the screen:

Wscript.Echo “<? xml version = ” & Chr(34) & “1.0” & Chr(34) & ” encoding = ” & _
    Chr(34) & “UTF-8″ & Chr(34) & ” ?>”

Give it a try and see what happens.

We mentioned a second method of writing quotes to a text file. You can perform this same trick by doubling up your quote marks:

Wscript.Echo “<? xml version = “”1.0″” encoding = “”UTF-8″” ?>”

This works just fine and, in this example, is easier than using Chr(34). The only reason we don’t recommend doubling up your quote marks is that, later on, you can get yourself into situations that look like this, with single and double quote marks intermingled (something that can happen quite a bit when writing more-complicated WMI queries):

“”‘” test “‘””

It’s next-to-impossible to look at code like that and figure out what’s what. We like Chr(34) because it makes the scripts much easier to read. But if you prefer double double quotes, this script will also work:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.CreateTextFile(“C:\Scripts\test.xml”)

objFile.WriteLine “<? xml version = “”1.0″” encoding = “”UTF-8″” ?>”

objFile.Close


0 comments

Discussion is closed.

Feedback usabilla icon