{"id":55833,"date":"2008-04-08T00:47:00","date_gmt":"2008-04-08T00:47:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/04\/08\/hey-scripting-guy-how-can-i-check-to-see-if-two-lines-of-text-are-in-a-file-and-if-they-arent-append-those-lines-to-the-file\/"},"modified":"2008-04-08T00:47:00","modified_gmt":"2008-04-08T00:47:00","slug":"hey-scripting-guy-how-can-i-check-to-see-if-two-lines-of-text-are-in-a-file-and-if-they-arent-append-those-lines-to-the-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-check-to-see-if-two-lines-of-text-are-in-a-file-and-if-they-arent-append-those-lines-to-the-file\/","title":{"rendered":"Hey, Scripting Guy! How Can I Check to See if Two Lines of Text are in a File and, If They Aren\u2019t, Append Those Lines to the 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! Let me first say that your stuff is great. Really helpful scripts mixed with great humor; you guys are pissa&#8217;s (Australian slang for \u201cfunny\u201d). I have a script that needs to open a file and check to see if two lines of text are in that file. If they are, then I leave the file alone; if they aren\u2019t, I need to append the two lines to the end of the file. Unfortunately, though, I can\u2019t get the script to work: it <I>always<\/I> appends the two lines, even if both those lines are already in the file. Can you help me?<BR>&#8212; GT<\/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, GT, and, g\u2019day; it\u2019s always nice to hear from an Aussie, you know, a bloke from the Back of Bourke, way out there in Woop Woop, as we like to say. No doubt you\u2019re a bit agro about this script that doesn\u2019t work right, you\u2019re probably as cross as a frog in a sock and you\u2019re thinking \u201cHave I gone troppo? Do I have kangaroos loose in the top paddock?\u201d But no drama, mate. Let\u2019s have a squizz here and give it a burl, eh? By the time we\u2019re through, she\u2019ll be apples, and you\u2019ll be happy as Larry.<\/P>\n<P>And best of all, this won\u2019t cost you big bikkies; it\u2019s on the house. We\u2019ll give you the mate\u2019s rate on this one.<\/P>\n<P>If we understand you correctly, you need to modify the All.js file that\u2019s used to configure the Firefox Web browser. That leads to an ovious question: are the Scripting Guys going to spit the dummy just because you\u2019re using Firefox? Well, admittedly, we\u2019re a bit gobsmacked that you aren\u2019t using Internet Explorer. But no worries; after all, we\u2019re all cobbers here, and if you want to use Firefox, well, we\u2019re just going to answer your question and not be stickybeaks as to <I>why<\/I> you\u2019re using Firefox rather than Internet Explorer. <\/P>\n<P>And that\u2019s no porky.<\/P>\n<P>As you noted in your email, when your script fossicks through the All.js file it\u2019s looking for these two lines of text:<\/P><PRE class=\"codeSample\">pref(&#8220;general.config.obscure_value&#8221;, 0);\npref(&#8220;general.config.filename&#8221;, &#8220;mozilla.cfg&#8221;);\n<\/PRE>\n<P>We need a script \u2013 and a corker of a script, not a bodgy one \u2013 that can determine whether those two lines of text are already in the file. If they aren\u2019t, the script needs to add those lines to All.js. In other words, we need a script that looks a lot like this:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForAppending = 8<\/p>\n<p>blnMissingA = True\nblnMissingB = True<\/p>\n<p>strTextA = &#8220;pref(&#8221; &amp; Chr(34) &amp; &#8220;general.config.obscure_value&#8221; &amp; chr(34) &amp; &#8220;, 0);&#8221;\nstrTextB = &#8220;pref(&#8221; &amp; Chr(34) &amp; &#8220;general.config.filename&#8221; &amp; chr(34) &amp; &#8220;, &#8221; &amp; Chr(34) &amp; &#8220;mozilla.cfg&#8221; &amp; Chr(34) &amp; &#8220;);&#8221;<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\All.js&#8221;, ForReading)<\/p>\n<p>Do Until objFile.AtEndOfStream\n    strLine = objFile.ReadLine\n    If InStr(strLine, strTextA) Then\n        blnMissingA = False\n    ElseIf InStr(strLine, strTextB) Then\n        blnMissingB = False\n    End If    \nLoop<\/p>\n<p>objFile.Close<\/p>\n<p>If blnMissingA  Then \n    strAdder = strTextA &amp; vbCrLf\nEnd If<\/p>\n<p>If blnMissingB Then \n    strAdder = strAdder &amp; strTextB &amp; vbCrLf\nEnd If <\/p>\n<p>If strAdder &lt;&gt; &#8220;&#8221; Then\n    Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\All.js&#8221;, ForAppending)\n    objFile.Write strAdder\n    objFile.Close\nEnd If\n<\/PRE>\n<P>Let\u2019s yabber on a bit about how this script works. As you can see, we start out by defining a pair of constants, ForReading and ForAppending; we\u2019ll need to use these two constants when working with the text file. We then assign the value True to a pair of variables (blnMissingA and blnMissingB). We\u2019re going to use these two variables to keep track of the two lines of text in question, and to indicate whether or not either of those lines is missing. We set the initial value to True because we haven\u2019t found either value yet (at least in part because we haven\u2019t started looking yet).<\/P>\n<P>And if you\u2019re thinking, \u201cThis script doesn\u2019t stand a Buckley\u2019s chance of working,\u201d well, be patient. By the time we finish, she\u2019ll be right.<\/P>\n<P>When you look at the next two lines of code you might think, \u201cHoly dooley! The Scripting Guys have come a gutser here!\u201d Actually, what we\u2019re doing here is pretty simple: in fact, all we\u2019re doing is assigning the two target lines of text to the variables strTextA and strTextB. It just <I>looks<\/I> like we were a bit stonkered when we wrote this code; that\u2019s because we need to insert double quote marks inside the strings. In fact, each instance of <B>Chr(34)<\/B> represents a double quote mark :<\/P><PRE class=\"codeSample\">strTextA = &#8220;pref(&#8221; &amp; Chr(34) &amp; &#8220;general.config.obscure_value&#8221; &amp; chr(34) &amp; &#8220;, 0);&#8221;\nstrTextB = &#8220;pref(&#8221; &amp; Chr(34) &amp; &#8220;general.config.filename&#8221; &amp; chr(34) &amp; &#8220;, &#8221; &amp; Chr(34) &amp; &#8220;mozilla.cfg&#8221; &amp; Chr(34) &amp; &#8220;);&#8221;\n<\/PRE>\n<P>In other words, in line 1 we\u2019re combining these elements and assigning the resulting string to the variable strTextA:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>pref(<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>&#8220;<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>general.config.obscure_value<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>&#8220;<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>, 0);<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>When you get right down to it, that\u2019s pretty bog standard, eh?<\/P>\n<P>After we\u2019ve defined the constants and variables our next step is to open the file All.js (which, for the purposes of this script, we\u2019ve stored in the folder C:\\Scripts). These two lines of code create an instance of the <B>Scripting.FileSystemObject<\/B>, then open the file C:\\Scripts\\All.js for reading:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\All.js&#8221;, ForReading)\n<\/PRE>\n<P>At this point all you blokes and sheilas can probably guess what comes next: we next set up a Do Until loop designed to read the file line-by-line:<\/P><PRE class=\"codeSample\">Do Until objFile.AtEndOfStream\n<\/PRE>\n<P>Inside that loop, we use the <B>ReadLine<\/B> method to read the first line in the file, assigning that value to a variable named strLine:<\/P><PRE class=\"codeSample\">strLine = objFile.ReadLine\n<\/PRE>\n<P>We then use the <B>InStr<\/B> function to see if this line of text contains the string value stored in the variable strTextA:<\/P><PRE class=\"codeSample\">If InStr(strLine, strTextA) Then\n<\/PRE>\n<P>If we happen to find the target text we then change the value of blnMissingA to False:<\/P><PRE class=\"codeSample\">blnMissingA = False\n<\/PRE>\n<P>Why do we do that? That\u2019s right: because it turns out that our first target string hasn\u2019t gone walkabout after all. Instead, we found the target value in the file All.js.<\/P>\n<P>We then do a similar check for the string value stored in the variable strTextB. Once that\u2019s done we go back to the top of the loop and repeat the process with the next line in the text file. As soon as we\u2019ve examined each and every line in the text file we close that file:<\/P><PRE class=\"codeSample\">objFile.Close\n<\/PRE>\n<P>So what comes next? Well, next we check to see if the value of blnMissingA is True. If it is, we then add the value of strTextA to a brand-new variable, a variable named strAdder. That\u2019s what this block of code is for:<\/P><PRE class=\"codeSample\">If blnMissingA Then \n    strAdder = strTextA &amp; vbCrLf\nEnd If\n<\/PRE>\n<P>Why do we do this? Well, if strTextA can\u2019t be found in All.js that means two things: 1) blnMissingA will still be true; and, 2) we need to add the value of strTextA to the text file. We then do a similar check for blnMissingB; if blnMissingB is true we add the value of strTextB to the variable strAdder:<\/P><PRE class=\"codeSample\">If blnMissingB Then \n    strAdder = strAdder &amp; strTextB &amp; vbCrLf\nEnd If\n<\/PRE>\n<P>What does all that mean? Well, if neither of our two target lines of text were found that means that strAdder will be equal to this:<\/P><PRE class=\"codeSample\">pref(&#8220;general.config.obscure_value&#8221;, 0);\npref(&#8220;general.config.filename&#8221;, &#8220;mozilla.cfg&#8221;);\n<\/PRE>\n<P>At this point we\u2019re within cooee of wrapping things up. To begin with, we check to see if strAdder is an empty string:<\/P><PRE class=\"codeSample\">If strAdder &lt;&gt; &#8220;&#8221; Then\n<\/PRE>\n<P>If it is, that can mean only one thing: both our target strings were already in the file All.js after all. If that\u2019s the case, then we\u2019re done. If it\u2019s <I>not<\/I> the case (that is, if strAdder is not an empty string) then we execute this block of code:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\All.js&#8221;, ForAppending)\nobjFile.Write strAdder\nobjFile.Close\n<\/PRE>\n<P>All we\u2019re doing here is reopening the file All.js, this time for appending. We use the <B>Write<\/B> method to append the value of strAdder to the file, then we close the file.<\/P>\n<P>In our opinion this script is bottler, but you might be wondering if it will work as advertised. Let\u2019s put our answer in terms that everyone can understand: it\u2019s London to a brick that this is going to work. Like we said, the Scripting Guys might look like dags, and we might act like we aren\u2019t worth a zack, but we\u2019re really just larrikins, and our answers are almost always fair dinkum.<\/P>\n<P>But, then again, you already knew that, didn\u2019t you? <\/P>\n<P>And now it\u2019s time for us to do the Harold Holt. Ta, mates; see you all tomorrow.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Let me first say that your stuff is great. Really helpful scripts mixed with great humor; you guys are pissa&#8217;s (Australian slang for \u201cfunny\u201d). I have a script that needs to open a file and check to see if two lines of text are in that file. If they are, then I [&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":[38,3,12,5],"class_list":["post-55833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Let me first say that your stuff is great. Really helpful scripts mixed with great humor; you guys are pissa&#8217;s (Australian slang for \u201cfunny\u201d). I have a script that needs to open a file and check to see if two lines of text are in that file. If they are, then I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55833","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=55833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55833\/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=55833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}