{"id":70163,"date":"2005-03-25T17:52:00","date_gmt":"2005-03-25T17:52:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/25\/how-can-i-write-double-quotes-to-a-text-file\/"},"modified":"2005-03-25T17:52:00","modified_gmt":"2005-03-25T17:52:00","slug":"how-can-i-write-double-quotes-to-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-write-double-quotes-to-a-text-file\/","title":{"rendered":"How Can I Write Double Quotes to a Text File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I\u2019m 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?<BR><BR>&#8212; JP<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, JP. According to your email, you\u2019re trying to write the following line to your XML file:<\/P><PRE class=\"codeSample\">&lt;? xml version = &#8220;1.0&#8221; encoding = &#8220;UTF-8&#8221; ?&gt;\n<\/PRE>\n<P>However, you\u2019re getting hung up on the double quotes that surround <B>1.0<\/B> and <B>UTF-8<\/B>. 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\u2019s 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:<\/P><PRE class=\"codeSample\">objFile.WriteLine &#8220;&lt;? xml version = &#8220;1.0&#8221; encoding = &#8220;UTF-8&#8243; ?&gt;&#8221;\n<\/PRE>\n<P>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: <B>&#8220;&lt;? xml version = &#8220;<\/B>. In other words, the string consists of everything embedded inside the first set of double quotes. Thus the script reads along, sees <B>&#8220;&lt;? xml version = &#8220;<\/B> and assumes that\u2019s the end of the string. However, you have some additional text following that string: <B>1.0&#8243; encoding = &#8220;UTF-8&#8243; ?&gt;&#8221;<\/B>. That\u2019s 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.<\/P>\n<P>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\u2019re going to focus on just one (although we\u2019ll briefly show you the other at the end of this column): we\u2019re going to use VBScript\u2019s <B>Chr<\/B> 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 <B>Chr(34)<\/B>; the Chr function takes the ASCII value &#8211; 34 &#8211; and converts it to an actual character (in this case double quotes).<\/P>\n<P>Trust us; it\u2019s much easier than it sounds. Here\u2019s a script that writes the desired line to the file C:\\Scripts\\Test.xml:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.CreateTextFile(&#8220;C:\\Scripts\\test.xml&#8221;)<\/p>\n<p>objFile.WriteLine &#8220;&lt;? xml version = &#8221; &amp; chr(34) &amp; &#8220;1.0&#8221; &amp; chr(34) &amp; _\n    &#8221; encoding = &#8221; &amp; chr(34) &amp; &#8220;UTF-8&#8243; &amp; chr(34) &amp; &#8221; ?&gt;&#8221;<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>We begin by creating an instance of the FileSystemObject and then calling the <B>CreateTextFile<\/B> 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:<\/P><PRE class=\"codeSample\">objFile.WriteLine &#8220;&lt;? xml version = &#8221; &amp; chr(34) &amp; &#8220;1.0&#8221; &amp; chr(34) &amp; _\n    &#8221; encoding = &#8221; &amp; chr(34) &amp; &#8220;UTF-8&#8243; &amp; chr(34) &amp; &#8221; ?&gt;&#8221;\n<\/PRE>\n<P>Yes, it looks weird, but it\u2019s really not that bad. All we\u2019re doing is calling the <B>WriteLine<\/B> method, and asking WriteLine to do the following.<\/P>\n<P>First, we need it to compose the string to be written, starting with the characters <B>&lt;? xml version = <\/B>. (Note the space after the equal sign.) After that, tack on the character with the ASCII value of 34. That happens to be this: <B>&#8220;<\/B>. Our string starts out looking like this: <\/P><PRE class=\"codeSample\">&lt;? xml version = &#8221;\n<\/PRE>\n<P>Of course, we\u2019re not done yet. Next, we add the characters <B>1.0<\/B> followed by another set of double quotes. That gives us a string that looks like this:<\/P><PRE class=\"codeSample\">&lt;? xml version = &#8220;1.0&#8221;\n<\/PRE>\n<P>Can you see where this is all headed? We add the phrase <B>encoding = <\/B>(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 <B>UTF-8<\/B> and another set of double quotes. In other words:<\/P><PRE class=\"codeSample\">&lt;? xml version = &#8220;1.0&#8221; encoding = &#8220;UTF-8&#8221;\n<\/PRE>\n<P>Now we just need to add the final chunk of characters:<B> ?&gt;&#8221;<\/B>. With the string fully composed, WriteLine will write the following to the XML file:<\/P><PRE class=\"codeSample\">&lt;? xml version = &#8220;1.0&#8221; encoding = &#8220;UTF-8&#8221; ?&gt;\n<\/PRE>\n<P>Make sense? All we did is take the string we want to write and surround it with double quotes:<\/P><PRE class=\"codeSample\">&#8220;&lt;? xml version = &#8220;1.0&#8221; encoding = &#8220;UTF-8&#8243; ?&gt;&#8221;\n<\/PRE>\n<P>As we know, that\u2019s not valid VBScript syntax, so we then replace each set of embedded quotes with <B>&amp; Chr(34) &amp;<\/B> (and making sure that all the other characters are surrounded by double quotes). For example, here we\u2019ve replaced the double quotes surrounding <B>1.0<\/B>:<\/P><PRE class=\"codeSample\">&#8220;&lt;? xml version = &#8221; &amp; Chr(34) &amp; &#8220;1.0&#8221; &amp; Chr(34) &amp; &#8221; encoding = &#8220;UTF-8&#8243; ?&gt;&#8221;\n<\/PRE>\n<P>And here we\u2019ve replaced the double quotes surround <B>UTF-8<\/B>:<\/P><PRE class=\"codeSample\">&#8220;&lt;? xml version = &#8221; &amp; Chr(34) &amp; &#8220;1.0&#8221; &amp; Chr(34) &amp; &#8221; encoding = &#8221; &amp; Chr(34) &amp; &#8220;UTF-8&#8243; &amp; Chr(34) &amp; &#8221; ?&gt;&#8221;\n<\/PRE>\n<P>To test this construction we can echo the finished product to the screen:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;&lt;? xml version = &#8221; &amp; Chr(34) &amp; &#8220;1.0&#8221; &amp; Chr(34) &amp; &#8221; encoding = &#8221; &amp; _\n    Chr(34) &amp; &#8220;UTF-8&#8243; &amp; Chr(34) &amp; &#8221; ?&gt;&#8221;\n<\/PRE>\n<P>Give it a try and see what happens. <\/P>\n<P>We mentioned a second method of writing quotes to a text file. You can perform this same trick by doubling up your quote marks:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;&lt;? xml version = &#8220;&#8221;1.0&#8243;&#8221; encoding = &#8220;&#8221;UTF-8&#8243;&#8221; ?&gt;&#8221;\n<\/PRE>\n<P>This works just fine and, in this example, is easier than using Chr(34). The only reason we don\u2019t 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):<\/P><PRE class=\"codeSample\">&#8220;&#8221;&#8216;&#8221; test &#8220;&#8216;&#8221;&#8221;\n<\/PRE>\n<P>It\u2019s next-to-impossible to look at code like that and figure out what\u2019s 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:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.CreateTextFile(&#8220;C:\\Scripts\\test.xml&#8221;)<\/p>\n<p>objFile.WriteLine &#8220;&lt;? xml version = &#8220;&#8221;1.0&#8243;&#8221; encoding = &#8220;&#8221;UTF-8&#8243;&#8221; ?&gt;&#8221;<\/p>\n<p>objFile.Close\n<\/PRE><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/mar05\/hey0325.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/mar05\/hey0325.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019m 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?&#8212; JP Hey, JP. According to your email, you\u2019re trying to write the following line to your XML file:&lt;? xml version = &#8220;1.0&#8221; [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,14,5],"class_list":["post-70163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I\u2019m 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?&#8212; JP Hey, JP. According to your email, you\u2019re trying to write the following line to your XML file:&lt;? xml version = &#8220;1.0&#8221; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70163"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70163\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}