{"id":66683,"date":"2006-08-15T21:48:00","date_gmt":"2006-08-15T21:48:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/15\/how-can-i-remove-double-double-quotes-from-a-text-file\/"},"modified":"2006-08-15T21:48:00","modified_gmt":"2006-08-15T21:48:00","slug":"how-can-i-remove-double-double-quotes-from-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-remove-double-double-quotes-from-a-text-file\/","title":{"rendered":"How Can I Remove Double Double Quotes From a Text File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! I have a comma-separated values file that has data similar to this: data1&#8243;,&#8221;&#8221;data2&#8243;&#8221;,&#8221;data3&#8243;,&#8221;&#8221;data4&#8243;&#8221;. As you can see, some of the items are enclosed by <I>two<\/I> sets of double quote marks (like &#8220;&#8221;data2&#8243;&#8221;). How can I remove one of those sets of double quote marks, leaving the item looking like this: \u201cdata2\u201d?<BR><BR>&#8212; RH<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, RH. Yes, it\u2019s true: Google\u2122 is busy sending cease-and-desist letters to various news organizations and the like, asking them to refrain from using \u201cgoogle\u201d as a verb. It\u2019s still OK to say, \u201cI did a Google Internet search for x.\u201d However, in their mind it\u2019s <I>not<\/I> OK to say, \u201cI googled x.\u201d<\/P>\n<P>We don\u2019t know whether Google will succeed in their quest; after all, Xerox\u00ae, Kleenex\u00ae, and Jell-O\u00ae all tried \u2013 not very successfully \u2013 to keep their brand names from becoming generic terms for photocopying, tissue, and gelatin, respectively. After giving this considerable thought, however, the Scripting Guys have decided that we would like to help Google out as best we can. Therefore, we are officially declaring that it\u2019s OK to use \u201cscripting guysed\u201d as a replacement verb for \u201cgoogled.\u201d If you don\u2019t like saying, \u201cI did a Google Internet search for x,\u201d then feel free to say \u201cI scripting guysed x.\u201d Consider it our little gift to the world.<\/P>\n<TABLE id=\"EDD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. We should hasten to add that we\u2019re not giving away <I>everything<\/I>, however. For example, don\u2019t even <I>think<\/I> about writing a daily column in which the answer never seems to have anything to do with the question. That\u2019s a Scripting Guys exclusive.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>OK, now, where were we? Oh, that\u2019s right: RH has a CSV file with data similar to this:<\/P><PRE class=\"codeSample\">&#8220;data1&#8243;,&#8221;&#8221;data2&#8243;&#8221;,&#8221;data3&#8243;,&#8221;&#8221;data4&#8243;&#8221;\n<\/PRE>\n<P>What RH would <I>like<\/I> to have is a CSV file with data similar to this, with just a single set of double quotes surrounding each item:<\/P><PRE class=\"codeSample\">&#8220;data1&#8243;,&#8221;data2&#8243;,&#8221;data3&#8243;,&#8221;data4&#8221;\n<\/PRE>\n<P>Can the Scripting Guys provide a solution to RH\u2019s problem? Let\u2019s put it this way: when your name is synonymous with searching the Internet, hey, you can do almost anything:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSo = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)<\/p>\n<p>strContents = objFile.ReadAll<\/p>\n<p>objFile.Close<\/p>\n<p>strFindText = Chr(34) &amp; Chr(34)\nstrReplaceText = Chr(34)<\/p>\n<p>strContents = Replace(strContents, strFindText, strReplaceText)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)<\/p>\n<p>objFile.WriteLine strContents<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>Now, admittedly, you could scripting guys the Internet and search for information about the various techniques we\u2019ve used in this script. But no need to do that; as long as we\u2019re here we\u2019ll go ahead and explain how the script works.<\/P>\n<P>To begin with, we define a pair of constants \u2013 ForReading and ForWriting \u2013 which we\u2019ll use when opening the text file. (And yes, we need two constants for this; that\u2019s because we\u2019ll need to open the file two different times, and in two different modes.) Next we create an instance of the <B>Scripting.FileSystemObject<\/B>, then use this line of code to open the file C:\\Scripts\\Test.txt for reading:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\n<\/PRE>\n<P>Once the file is open we then use the <B>ReadAll<\/B> method to read in the entire file and store the contents in a variable named strContents:<\/P><PRE class=\"codeSample\">strContents = objFile.ReadAll\n<\/PRE>\n<P>And yes, there <I>is<\/I> a good reason why we do that: the FileSystemObject doesn\u2019t allow us to directly manipulate the text file itself. Because of that, we need to read the file and store the contents in memory; we can then do a search-and-replace operation on that copy of the file. After we\u2019ve made the required changes we\u2019ll reopen the file \u2013 this time for writing \u2013 and overwrite the existing contents with the new, modified contents currently in memory.<\/P>\n<P>Got that? Good. If that\u2019s the case, then you\u2019ll understand why, after reading from the file, we immediately close it: with the file open for reading, we can no longer do anything with it. Instead, we have to close it, then reopen it for writing. (Yes, that <I>is<\/I> kind of silly. But that\u2019s the way the FileSystemObject works.)<\/P>\n<P>Now we\u2019re ready to do a little searching and replacing; what we\u2019re going to do is replace any set of double quotes \u2013 &#8220;&#8221; \u2013 with a set of single quotes: &#8220;. To do that, however, we\u2019re going to use the <B>Chr<\/B> function to indicate the ASCII value of the characters we\u2019re searching for. We do that to help prevent anyone who reads our script from getting a headache; because double quotes are a reserved character in VBScript the only way to search for double double quotes is to store those double double quotes inside <I>another<\/I> pair of double double quotes. Something like this:<\/P><PRE class=\"codeSample\">&#8220;&#8221;&#8221;&#8221;&#8221;&#8221;\n<\/PRE>\n<P>That\u2019s way too many double quotes for the Scripting Guys to deal with. Therefore, we sidestep the problem of embedding quotes within quotes within quotes by, instead, searching for consecutive instances of Chr(34):<\/P><PRE class=\"codeSample\">strFindText = Chr(34) &amp; Chr(34)\n<\/PRE>\n<P>As you might have guessed, Chr(34) is equivalent to the double quote mark: &#8220;. Consequently, Chr(34) &amp; Chr(34) will be equal to the very thing we\u2019re searching for: &#8220;&#8221;.<\/P>\n<P>Of course, we don\u2019t just want to <I>find<\/I> a pair of double quote marks; we want to replace each of these pairs with a single set of double quote marks. Therefore, in addition to defining the target text, we also need to define the replacement text. Needless to say, that happens to be a single instance of Chr(34):<\/P><PRE class=\"codeSample\">strReplaceText = Chr(34)\n<\/PRE>\n<P>Now, at long last, we can call the <B>Replace<\/B> function, passing this function \u2013 in order \u2013 the string value we want to search (the variable strContents); the target text we want to search for (strFindText); and the text we want to use to replace the target text (strReplaceText). That all sounds very complicated, but it really requires just a single line of code:<\/P><PRE class=\"codeSample\">strContents = Replace(strContents, strFindText, strReplaceText)\n<\/PRE>\n<P>Execute this line of code, and the variable strContents will contain a corrected copy of the text file, a copy that has all the doubled-up double quotes removed. Once we have that we can reopen the file Test.txt, this time for writing:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\n<\/PRE>\n<P>The rest is easy: we call the <B>WriteLine<\/B> method to replace the existing contents of the file with the value stored in strContents, and then close the file. <\/P>\n<P>That\u2019s really all there is to it. We\u2019re just going to walk over to the photo copy machine and scripting guys this column, then maybe have a dish of strawberry Scripting Guys (of <I>course<\/I> with whipped cream on it). See you tomorrow.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a comma-separated values file that has data similar to this: data1&#8243;,&#8221;&#8221;data2&#8243;&#8221;,&#8221;data3&#8243;,&#8221;&#8221;data4&#8243;&#8221;. As you can see, some of the items are enclosed by two sets of double quote marks (like &#8220;&#8221;data2&#8243;&#8221;). How can I remove one of those sets of double quote marks, leaving the item looking like this: \u201cdata2\u201d?&#8212; RH [&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":[40,3,4,14,5],"class_list":["post-66683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-filesystemobject","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a comma-separated values file that has data similar to this: data1&#8243;,&#8221;&#8221;data2&#8243;&#8221;,&#8221;data3&#8243;,&#8221;&#8221;data4&#8243;&#8221;. As you can see, some of the items are enclosed by two sets of double quote marks (like &#8220;&#8221;data2&#8243;&#8221;). How can I remove one of those sets of double quote marks, leaving the item looking like this: \u201cdata2\u201d?&#8212; RH [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66683","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=66683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66683\/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=66683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}