{"id":65193,"date":"2007-03-30T03:09:00","date_gmt":"2007-03-30T03:09:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/30\/how-can-i-locate-a-word-in-a-text-file-and-then-echo-back-that-word-and-any-remaining-text\/"},"modified":"2007-03-30T03:09:00","modified_gmt":"2007-03-30T03:09:00","slug":"how-can-i-locate-a-word-in-a-text-file-and-then-echo-back-that-word-and-any-remaining-text","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-locate-a-word-in-a-text-file-and-then-echo-back-that-word-and-any-remaining-text\/","title":{"rendered":"How Can I Locate a Word in a Text File and Then Echo Back That Word and Any Remaining Text?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! How can I search for a word in a text file and then, after I find it, echo back that word and any remaining text in the text file?<BR><BR>&#8212; KA<\/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, KA. Before we begin we\u2019d like to thank the hundreds and hundreds of people who have sent Scripting Guy Jean Ross get well wishes after her <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/mms2007\/tuesday.mspx\"><B>unfortunate mishap<\/B><\/A> at Microsoft Management Summit 2007. Not that we\u2019ve actually <I>received<\/I> any get well wishes, mind you. But we have no doubt that they are all on the way. Consider this a thank you in advance.<\/P>\n<P>Incidentally, and while we\u2019re on the subject, there is a rumor going around that Jean slipped in her hotel room, fell, and cracked her head open. That is definitely <I>not<\/I> what happened. Instead, Jean arrived at the conference at the very moment the San Diego Convention Center was surrounded by hundreds of ninja warriors, all clad entirely in black except for a small penguin logo just above the heart. While her fellow Microsoft employees cowered in fear, begging for their lives, Jean \u2013 oblivious to the danger and willing to sacrifice everything for the good of both Microsoft and all of humanity \u2013 leaped into the fray, single-handedly defeating the entire army. <\/P>\n<P>Not that she emerged unscathed, mind you; in fact, at one point a ninja shot a flaming arrow that lodged itself deep into Jean\u2019s skull. Of course, one flaming arrow in the skull isn\u2019t going to stop Scripting Hero Jean Ross. Instead, she simply yanked the arrow from her head and continued the battle. It was only many hours later, after she knew that everyone was safe and that the Summit had been saved, that she consented to go to the emergency room and receive first aid. <I>That\u2019s<\/I> what really happened to Scripting Guy Jean Ross.<\/P>\n<P>Or so she says, anyway. <\/P>\n<P>So why would a band of ninja warriors attack the Microsoft Management Summit? To be honest, we don\u2019t know. However, we suspect they wanted to shut down the conference before Microsoft could make the week\u2019s most important announcement: the fact that, using a script, it\u2019s possible to locate a specific word in a text file and then echo back just that word and any subsequent text in the file. In fact, you can do so using <I>this<\/I> script:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/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\nobjFile.Close<\/p>\n<p>arrWords = Split(strContents, &#8221; &#8220;)\nintStart = 0<\/p>\n<p>For Each strWord in arrWords\n    If InStr(strWord, &#8220;telescope&#8221;) Then\n        intStart = intStart + 1\n    End If<\/p>\n<p>    If intStart &gt; 0 Then\n        strFinalText = strFinalText &amp; strWord &amp; &#8221; &#8221;\n    End If\nNext<\/p>\n<p>Wscript.Echo strFinalText\n<\/PRE>\n<P>Yes, you <I>can<\/I> see why the ninjas wouldn\u2019t want word of this to leak out, can\u2019t you? And don\u2019t worry: the Scripting Guys are not afraid to explain how this script works, either. However, before we do that we should note that, for this particular example, we\u2019re assuming that we have a text file that just happens to consist of a quotation from <I>Alice in Wonderland<\/I>:<\/P><PRE class=\"codeSample\">Curiouser and curiouser!&#8217; cried Alice (she was so much surprised, that \nfor the moment she quite forgot how to speak good English); &#8216;now I&#8217;m \nopening out like the largest telescope that ever was! Good-bye, feet!&#8217; \n(for when she looked down at her feet, they seemed to be almost out of \nsight, they were getting so far off).\n<\/PRE>\n<P>Our task is to locate the word <I>telescope<\/I> in this file and, if and when we find the word, echo back the target word <I>telescope<\/I> and any subsequent text in the file. In other words, our script should echo back the following:<\/P><PRE class=\"codeSample\">telescope that ever was! Good-bye, feet!&#8217; \n(for when she looked down at her feet, they seemed to be almost out of \nsight, they were getting so far off).\n<\/PRE>\n<P>And guess what: that\u2019s exactly what our script <I>does<\/I> do! Now all we have to do is try and figure out <I>how<\/I> it does that.<\/P>\n<P>To begin with, we define a constant named ForReading and set the value to 1; we\u2019ll use that constant when we go to open our text file. Speaking of which, we next use the following two lines of code to create an instance of the <B>Scripting.FileSystem<\/B> object and to open the file C:\\Scripts\\Test.txt:<\/P><PRE class=\"codeSample\">Set obJFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\n<\/PRE>\n<P>As soon as the file is open we use the <B>ReadAll<\/B> method to read the entire contents into a variable named strContents, and then just as quickly close the file. (Why? Because, with the contents safely tucked away in the variable strContents we no longer need the actual file itself.)<\/P>\n<P>OK; now what do we do? Well, using a trick we employed during the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/solutions07\/apssol06.mspx\"><B>2007 Winter Scripting Games<\/B><\/A>, we next use the VBScript <B>Split<\/B> function to split the value of strContents on the blank space:<\/P><PRE class=\"codeSample\">arrWords = Split(strContents, &#8221; &#8220;)\n<\/PRE>\n<P>What does that give us? That gives us an array (arrWords) consisting of all the individual words (plus any punctuation marks) in the text file, an array that starts out like this:<\/P><PRE class=\"codeSample\">Curiouser \nand \ncuriouser!&#8217; \ncried\n<\/PRE>\n<P>Etc., etc.<\/P>\n<P>Once we have the array we then set the value of a counter variable named intStart to 0:<\/P><PRE class=\"codeSample\">intStart = 0\n<\/PRE>\n<P>You\u2019re right: at this point in time this <I>doesn\u2019t<\/I> make much sense, does it? But don\u2019t fret; everything should become crystal-clear in just a moment. After assigning a value to intStart we next set up a For Each loop to loop through all the items (words) in the array arrWords. What\u2019s the first thing we do inside the loop? The first thing we do inside that loop is check to see if the value of the current array item contains the string <I>telescope<\/I>:<\/P><PRE class=\"codeSample\">If InStr(strWord, &#8220;telescope&#8221;) Then\n<\/PRE>\n<P>Let\u2019s pretend that the very first word in our text file really <I>is<\/I> the target word telescope<I>.<\/I> What do we do then? Well, all we do is increment the value of intStart by 1:<\/P><PRE class=\"codeSample\">intStart = intStart + 1\n<\/PRE>\n<P>Why do we do that? To answer that question, take a look at the next block of code:<\/P><PRE class=\"codeSample\">If intStart &gt; 0 Then\n    strFinalText = strFinalText &amp; strWord &amp; &#8221; &#8221;\nEnd If\n<\/PRE>\n<P>As you can see, if intStart is greater than 0 we\u2019re going to assign a value to a variable named strFinalText. That value is going to consist of the current value of strFinalText plus the current array item (strWord) plus a blank space. (Why a blank space? Because that enables us to restore all the blank spaces that disappeared when we called the Split method.)<\/P>\n<P>See what we\u2019re doing here? The first word in our text (and thus the first item in the array arrWords) is the word <I>Curiouser<\/I>. Does the word <I>Curiouser<\/I> contain the string <I>telescope<\/I>? No, it doesn\u2019t; therefore the value of intStart remains 0. In turn, that means we won\u2019t assign a new value to strFinalText; after all, we do that only if intStart is greater than 0. What about the second word in the file: <I>and<\/I>? Nope, no match there either. And so, once again, we loop around and try the next word.<\/P>\n<P>Of coruse, sooner or later we <I>will<\/I> hit the word <I>telescope<\/I>; at that point we\u2019ll change the value of intStart to 1 and assign a new value to strFinalText. And guess what? From that moment on intStart will <I>always<\/I> be greater than 1. In turn, that means that all the remaining words in the file will be assigned to strFinalText; <I>that\u2019s<\/I> how we manage to capture both the target word and any subsequent text in the file. When we\u2019ve finished looping through the array we finish the script by reporting back the value of strFinalText, a value that should look just like this:<\/P><PRE class=\"codeSample\">telescope that ever was! Good-bye, feet!&#8217; \n(for when she looked down at her feet, they seemed to be almost out of \nsight, they were getting so far off).\n<\/PRE>\n<P>And <I>that<\/I>, ninja warriors or no ninja warriors, is the answer the world has been waiting for. Whew!<\/P>\n<P>Now we know what you\u2019re thinking. And, in all fairness, at first the other Scripting Guys were a little skeptical of this whole ninja thing, too. But then we realized something: Jean has been on the team for a little over 2 years now, and during that time none of the Scripting Guys has ever been attacked by ninja warriors, <I>not even once<\/I>! <\/P>\n<P>Man, she <I>is<\/I> good, isn\u2019t she?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I search for a word in a text file and then, after I find it, echo back that word and any remaining text in the text file?&#8212; KA Hey, KA. Before we begin we\u2019d like to thank the hundreds and hundreds of people who have sent Scripting Guy Jean Ross [&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-65193","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! How can I search for a word in a text file and then, after I find it, echo back that word and any remaining text in the text file?&#8212; KA Hey, KA. Before we begin we\u2019d like to thank the hundreds and hundreds of people who have sent Scripting Guy Jean Ross [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65193","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=65193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65193\/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=65193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}