{"id":69273,"date":"2005-08-01T10:40:00","date_gmt":"2005-08-01T10:40:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/08\/01\/how-can-i-search-for-two-items-in-a-text-file\/"},"modified":"2005-08-01T10:40:00","modified_gmt":"2005-08-01T10:40:00","slug":"how-can-i-search-for-two-items-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-search-for-two-items-in-a-text-file\/","title":{"rendered":"How Can I Search for Two Items in 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! You\u2019ve shown us how to search a text file for a single word or phrase, but how can I search a text file for two phrases? I need to know if a file contains either <B>Windows 2000 <\/B><I>or <\/I><B>Windows XP<\/B>.<BR><BR>&#8212; JR<\/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, JR. You know, it\u2019s hard enough to get the Scripting Guys to do <I>one<\/I> thing; getting them to do <I>two<\/I> things would seem to be well-nigh impossible. But we\u2019ll tell you what: as long as you don\u2019t mind us showing you an <I>easy<\/I> way to search for multiple items in a text file then we\u2019ll show you how to search for multiple items in a text file.<\/P>\n<TABLE id=\"EJD\" 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>. Why is this the \u201ceasy way?\u201d Well, we\u2019re not going to bother setting up an array or some other complicated framework for carrying out multiple searches. Instead we\u2019re going to search the file once for the first term, then search it a second time for the second term. It\u2019s not elegant, but it\u2019s easy, and it works.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Here\u2019s a simple little script that can tell you if either the term <B>Windows 2000 <\/B><I>or<\/I> the term <B>Windows XP<\/B> can be found in the text file C:\\Scripts\\Text.txt:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nblnFound = False<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\nstrContents = objFile.ReadAll\nobjFile.Close<\/p>\n<p>If InStr(strContents, &#8220;Windows 2000&#8221;) Then\n    blnFound = True\nEnd If<\/p>\n<p>If InStr(strContents, &#8220;Windows XP&#8221;) Then\n    blnFound = True\nEnd If<\/p>\n<p>If blnFound Then\n    Wscript.Echo &#8220;Either Windows 2000 or Windows XP appears in this file.&#8221;\nElse\n    Wscript.Echo &#8220;Neither Windows 2000 nor Windows XP appears in this file.&#8221;\nEnd If\n<\/PRE>\n<P>This script begins by defining a constant named ForReading and setting the value to 1; we\u2019ll need to use this when we open our text file. We also create a variable named blnFound and assign it the value False; we\u2019ll use this to keep track of whether or not either of our search terms are found in the file. If we find at least one of the terms we\u2019ll change the value of blnFound to True; otherwise, the value of blnFound will remain False.<\/P>\n<P>Next we open the file C:\\Scripts\\Test.txt for reading, then use the <B>ReadAll<\/B> method to read the entire contents of the file into a variable named strContents; we\u2019ll actually conduct our searches on this \u201ccopy\u201d of the file stored in memory. Because we don\u2019t need the physical file any more, we call the <B>Close<\/B> method to close the file.<\/P>\n<P>Now we\u2019re ready for our first search. This line of code uses the <B>InStr<\/B> function to determine whether the string <I>Windows 2000<\/I> can be found anywhere in the variable strContents:<\/P><PRE class=\"codeSample\">If InStr(strContents, &#8220;Windows 2000&#8221;) Then\n<\/PRE>\n<P>If InStr is True, then we set the value of blnFound to True; if InStr is False we simply skip to the next search. In that next search we repeat the process, this time searching for the string <I>Windows XP<\/I>:<\/P><PRE class=\"codeSample\">If InStr(strContents, &#8220;Windows XP&#8221;) Then\n<\/PRE>\n<P>If we found either <I>Windows 2000<\/I> or <I>Windows XP<\/I> (or both), blnFound will beTrue; if we didn\u2019t find either phrase, then blnFound will still be False. At the end of the script we check the value of blnFound, and indicate whether or not one or more of the search phrases were found in the file.<\/P>\n<P>But what if you need to know if the file contains <I>both<\/I> of the search phrases? We won\u2019t explain this is any real detail, but here\u2019s a script that will tell you whether or not both of the target phrases can be found in the file:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nintFound = 0<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\nstrContents = objFile.ReadAll\nobjFile.Close<\/p>\n<p>If InStr(strContents, &#8220;Windows 2000&#8221;) Then\n    intFound = intFound + 1\nEnd If<\/p>\n<p>If InStr(strContents, &#8220;Windows XP&#8221;) Then\n    intFound = intFound + 1\nEnd If<\/p>\n<p>If intFound = 2 Then\n    Wscript.Echo &#8220;The text file contains both Windows 2000 and Windows XP.&#8221;\nElse\n    Wscript.Echo &#8220;The text file does not contain both Windows 2000 and Windows XP.&#8221;\nEnd If\n<\/PRE>\n<P>Yes, it <I>is<\/I> similar to our previous script. The big difference is that we don\u2019t use a True-False variable; instead we use a counter variable named intFound. The script first searches for <I>Windows 2000<\/I>; if the phrase is found then intFound is incremented by 1. (Because intFound starts off at 0, that means intFound would now be equal to 1.) <\/P>\n<P>The script then searches for <I>Windows XP<\/I> and, if the phrase is found, increments the value of intFound by 1. The net result? At the end of the script, intFound will equal 2 only if both target phrases were found; if intFound equals 0 or 1, then either neither target phrase or only one target phrase was found. All that\u2019s left at that point is to echo back the results of our search.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! You\u2019ve shown us how to search a text file for a single word or phrase, but how can I search a text file for two phrases? I need to know if a file contains either Windows 2000 or Windows XP.&#8212; JR Hey, JR. You know, it\u2019s hard enough to get the Scripting [&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-69273","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! You\u2019ve shown us how to search a text file for a single word or phrase, but how can I search a text file for two phrases? I need to know if a file contains either Windows 2000 or Windows XP.&#8212; JR Hey, JR. You know, it\u2019s hard enough to get the Scripting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69273","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=69273"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69273\/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=69273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}