{"id":66103,"date":"2006-11-06T11:59:00","date_gmt":"2006-11-06T11:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/11\/06\/how-can-i-extract-the-text-between-the-header-and-footer-in-a-text-file\/"},"modified":"2006-11-06T11:59:00","modified_gmt":"2006-11-06T11:59:00","slug":"how-can-i-extract-the-text-between-the-header-and-footer-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-extract-the-text-between-the-header-and-footer-in-a-text-file\/","title":{"rendered":"How Can I Extract the Text Between the Header and Footer in a Text File?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! How can I extract the text between the header and footer in a text file?<\/p>\n<p>&#8212; LL<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, LL. Before we answer your question we should point out that, depending on when you\u2019re reading this, you might be missing Day 1 of <a href=\"http:\/\/null\/technet\/scriptcenter\/webcasts\/ps.mspx\"><b>Windows PowerShell Week<\/b><\/a>. Even as we speak the lovely and talented Jean Ross could be presenting an introduction to Windows PowerShell, Microsoft\u2019s new command shell\/scripting technology.<\/p>\n<p>Well, OK: right now the <i>talented<\/i> Jean Ross could be presenting an introduction to Windows PowerShell, Microsoft\u2019s new command shell\/scripting technology.<\/p>\n<p>Fine: right now Jean Ross could be presenting an introduction to Windows PowerShell, Microsoft\u2019s new command shell\/scripting technology. <\/p>\n<p>Is that better?<\/p>\n<p>But don\u2019t fret if you somehow missed today\u2019s presentation; all the webcasts will be available in archive form within the next 2 or 3 days. Besides, we still have four more webcasts to go; for example, tomorrow the lovely and talented Dean Tsaltas will explain the whys and wherefores of Windows PowerShell Cmdlets.<\/p>\n<p>\u2026.<\/p>\n<p>Sorry. We expected somebody to protest the fact that we referred to Dean as being both lovely and talented, but everyone seems fine with that description. Good news, Dean: the scripting world loves you!<\/p>\n<p>Of course, some of you \u2013 namely you, LL \u2013 might not care about any of that; you just want to know how to extract the text between the header and footer of a text file. Well, then why didn\u2019t you say so:<\/p>\n<pre class=\"codeSample\">Const ForReading = 1\nx = 0\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\nSet objFile = objFSO.OpenTextFile(\"C:\\Scripts\\Test.txt\", ForReading)\nDo Until objFile.AtEndOfStream\n    strLine = objFile.ReadLine\n    If Left(strLine, 4) = \"****\" Then\n        x = x + 1\n    End If\n    If x = 2 Then\n        Exit Do\n    End If\n    If Left(strLine, 4) &lt;&gt; \"****\" And x = 1 Then\n        strText = strText &amp; strLine &amp; vbCrLf\n    End If\nLoop\nWscript.Echo strText\n<\/pre>\n<p>A note of explanation before we begin. The text file that LL referred to uses a line of asterisks to mark the beginning and end of the file header. In other words, the text file looks something like this:<\/p>\n<pre class=\"codeSample\">******************************************************************\nThis is line 1 of the header.\nThis is line 2 of the header.\n******************************************************************\nThis is text that we don't care about.\nThis, too, is text we don\u2019t care about.\nAs is this.\n<\/pre>\n<p>Our job is to pull out just the two lines wedged between the asterisks. You know, these two lines:<\/p>\n<pre class=\"codeSample\">This is line 1 of the header.\nThis is line 2 of the header.\n<\/pre>\n<p>Will our script be able to pull off such a feat? Let\u2019s find out.<\/p>\n<p>As you can see, we start out by defining a constant named ForReading and setting the value to 1; we\u2019ll need this constant when we open our text file. We then set the value of a mysterious variable named <i>x<\/i> to 0. What do we need this variable for? You\u2019ll find out soon enough.<\/p>\n<p>What do you mean that isn\u2019t soon enough? We understand how exciting this is, but try to be patient. Remember: good things come to those who wait.<\/p>\n<p>Our next step is to create an instance of the <b>Scripting.FileSystemObject<\/b>, then use the <b>OpenTextFile<\/b> method to open the file C:\\Scripts\\Test.txt for reading. That&#8217;s what we do with these two lines of code:<\/p>\n<pre class=\"codeSample\">Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\nSet objFile = objFSO.OpenTextFile(\"C:\\Scripts\\Test.txt\", ForReading)\n<\/pre>\n<p>After the file has been opened we set up a Do Until loop that runs until we reach the end of the file (or, to make it sound like we know what we\u2019re talking about, until the <b>AtEndOfStream<\/b> property is True). The first thing we do inside that loop is use the <b>ReadLine<\/b> method to read the first line of the text file and then store that value in a variable named strLine:<\/p>\n<pre class=\"codeSample\">strLine = objFile.ReadLine\n<\/pre>\n<p>Most likely the first line in the text file is a row of asterisks that marks the beginning of the header. However, we don\u2019t know that for sure. Therefore, we use this line of code to verify that the first four characters in the line are, indeed, asterisks:<\/p>\n<pre class=\"codeSample\">If Left(strLine, 4) = \"****\" Then\n<\/pre>\n<table id=\"ENE\" 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\u2019re guessing that you don\u2019t have any other lines in the text file that begin with four asterisks. If you <i>do<\/i>, then you\u2019ll have to adjust the preceding line of code to take that into account (for example, checking to see if the first 10 or the first 20 or the first whatever characters are all asterisks).<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Let\u2019s assume that the first four characters of the line <i>are<\/i> all asterisks. (If they aren\u2019t, well, no big deal; the script is designed to handle that.) If we <i>have<\/i> encountered a line of asterisks that means that we\u2019ve hit the line that signifies the beginning of the header. To help us mark this momentous occasion we increment the value of x by 1. In other words, when x equals 1 we know that we&#8217;ve found the header.<\/p>\n<p>That brings us to this block of code:<\/p>\n<pre class=\"codeSample\">If x = 2 Then\n    Exit Do\nEnd If\n<\/pre>\n<p>What\u2019s <i>this<\/i> for? Well, when we reach the line in the text file that marks the beginning of the header we increment the value of x by 1. And guess what? We do the very same thing when we reach the line in the text file that marks the <i>end<\/i> of the header. (Why? Because that line also begins with four asterisks.) That means when we reach the end of the header x will be equal to 2; logically then, if x is equal to 2 we must have reached the end of the header. And because we <i>have<\/i> reached the end of the header then there&#8217;s no point in going on; therefore, we use the <b>Exit Do<\/b> statement to exit the Do Until loop.<\/p>\n<p>Make sense? The variable x is equal to 0 until we hit the line that marks the beginning of the header. At that point x gets set to 1 and remains at 1 until we reach the line marking the end of the header. The value of x will then get upped to 2, which is a signal to the script that it\u2019s time to exit the loop.<\/p>\n<p>Of course, the first time through the loop x won\u2019t be equal to 2; most likely it will be equal to 1. That brings us to this block of code:<\/p>\n<pre class=\"codeSample\">If Left(strLine, 4) &lt;&gt; \"****\" And x = 1 Then\n    strText = strText &amp; strLine &amp; vbCrLf\nEnd If\n<\/pre>\n<p>Here we\u2019re checking two conditions: that x is equal to 1 (which means we\u2019re dealing with the header) <i>and<\/i> that the first four characters in the line are not equal to ****. If both of these conditions are true then we\u2019re actually dealing with the header text, the very text we\u2019re trying to extract. With that in mind we grab the current line (strLine) and append that value plus a carriage return-linefeed character to a variable named strText. We then loop around and repeat the process with the next line in the text file. When we\u2019re all done, we echo back the value of strText.<\/p>\n<p>Admittedly, it\u2019s a tad bit complicated, but it works. In fact, here&#8217;s what we get back when we run the script:<\/p>\n<pre class=\"codeSample\">This is line 1 of the header.\nThis is line 2 of the header.\n<\/pre>\n<p>Like we hinted at, this script works even if the header doesn&#8217;t appear right at the very beginning of the document. To see what we mean, try running the script against the following text file and see what happens:<\/p>\n<pre class=\"codeSample\">This line we don't care about.\n******************************************************************\nThis is line 1 of the header.\nThis is line 2 of the header.\n******************************************************************\nThis is text that we don't care about.\nThis, too, is text we don\u2019t care about.\nAs is this.\n<\/pre>\n<p>And now, if you\u2019ll excuse us, we need to get back to the Windows PowerShell Week webcasts. Could this be the day that the lovely and talented Peter Costantini breaks into song? You&#8217;ll find out soon enough.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I extract the text between the header and footer in a text file? &#8212; LL Hey, LL. Before we answer your question we should point out that, depending on when you\u2019re reading this, you might be missing Day 1 of Windows PowerShell Week. Even as we speak the lovely and [&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,21,14,5],"class_list":["post-66103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I extract the text between the header and footer in a text file? &#8212; LL Hey, LL. Before we answer your question we should point out that, depending on when you\u2019re reading this, you might be missing Day 1 of Windows PowerShell Week. Even as we speak the lovely and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66103","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=66103"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66103\/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=66103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}