{"id":64483,"date":"2007-07-11T00:40:00","date_gmt":"2007-07-11T00:40:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/11\/how-can-i-insert-a-page-break-in-a-text-file-after-each-line-where-the-only-character-is-the-number-1\/"},"modified":"2007-07-11T00:40:00","modified_gmt":"2007-07-11T00:40:00","slug":"how-can-i-insert-a-page-break-in-a-text-file-after-each-line-where-the-only-character-is-the-number-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-insert-a-page-break-in-a-text-file-after-each-line-where-the-only-character-is-the-number-1\/","title":{"rendered":"How Can I Insert a Page Break in a Text File After Each Line Where the Only Character is the Number 1?"},"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! How can I open a text file and insert a page break after each line where the only character is the number 1?<BR><BR>&#8212; JR <\/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, JR. OK, to begin with, there\u2019s no doubt that the Scripting Guy who writes this column doesn\u2019t keep up with the world of fashion; for example, he considers \u201cgetting dressed up\u201d to mean going out without a baseball hat, something he does only for a wedding, a funeral, or a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/wednesday.mspx\"><B>good old-fashioned drenching<\/B><\/A>. And there\u2019s no doubt that the Scripting Guy who writes this column has some \u2026 interesting \u2026 ideas when it comes to fashion; the Scripting Editor, who has had occasion to listen to some of these rants, can vouch for that. (Or maybe not. Usually when he starts rambling on about why men shouldn\u2019t wear sandals she always remembers that she left a pot of water boiling on the stove and leaves.)<\/P>\n<P>Still, picture this if you will. (Or if you dare). A brisk summer morning in Seattle; the Scripting Guy who writes this column is sitting in the stands waiting for the Scripting Son\u2019s baseball game to start. (And because you\u2019re all <I>dying<\/I> to know this, yes, the Scripting Son pitched three innings of relief and picked up the win in that game.) In walks a guy wearing:<\/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>A fleece jacket.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Blue jean cutoff shorts. (And cut off a little too short, if you ask us.)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>A cowboy hat.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Cowboy boots.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>So was there anything wrong with that? Admittedly, there probably <I>isn\u2019t<\/I> anything wrong with that. But maybe there <I>should<\/I> be.<\/P>\n<P>Oh, well; to each his own, right? Besides, what does that have to do with inserting page breaks in a text file? Darned if we know. But at least the following script <I>does<\/I> have something to do with inserting page breaks in a text file:<\/P><PRE class=\"codeSample\">Const wdReplaceAll = 2<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.txt&#8221;)\nSet objSelection = objWord.Selection\nobjSelection.Find.Text = &#8220;^p1^p&#8221;\nobjSelection.Find.Forward = TRUE\nobjSelection.Find.Replacement.Text = &#8220;^p1^p^m&#8221;<\/p>\n<p>objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll\n<\/PRE>\n<P>First things first. To begin with, you might have noticed that this script uses Microsoft Word to open the text file and to insert the page breaks. Why? Well, as far as we know, the only way to insert page breaks into a text file is to use an application like Word; the FileSystemObject can\u2019t insert page breaks into a text file, and the text file wouldn\u2019t know what to do with those page breaks anyway. (Try finding the <I>Insert Page Break<\/I> command in Notepad. That\u2019s going to be a little difficult, to say the least.) Because of that we need to open the text file in Word, insert the page breaks, and then save that file as a Word document. (That\u2019s a step we left out in our sample script, but we have <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr05\/hey0401.mspx\"><B>at least one article<\/B><\/A> that tells how to save a file as a Word document.) Admittedly we no longer have a text file at that point, but we don\u2019t know any way to work around that.<\/P>\n<P>All right. Having done first things first, let\u2019s now do second things second and explain how the script actually works. For starters, we define a constant named wdReplaceAll and set the value to 2; we\u2019ll explain what that\u2019s for in just a second. We then create an instance of the <B>Word.Application<\/B> object and set the <B>Visible<\/B> property to True; that gives us a running instance of Microsoft Word that we can see on screen. After that we use these two lines of code to open the file C:\\Scripts\\Test.txt and create an instance of Word\u2019s <B>Selection<\/B> object:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.txt&#8221;)\nSet objSelection = objWord.Selection\n<\/PRE>\n<P>Why do we need an instance of Word\u2019s Selection object? That\u2019s easy: in order to do a search-and-replace operation (which is what we\u2019re about to do) we need to use the <B>Find<\/B> object. And Find just happens to be a child object of the Selection object.<\/P>\n<P>Still with us? Good. As it turns out, JR has a text file that looks something like this:<\/P><PRE class=\"codeSample\">Here&#8217;s some text.\n1\nHere\u2019s some more text.\n1\nHere&#8217;s even more text.\nAnd more text.\n1\nAnd here&#8217;s the last bit of text.\n<\/PRE>\n<P>Notice the lines that have nothing but the number 1 on them? Our task is to insert a page break after each of those lines. <\/P>\n<P>And just how do we propose to do that? We\u2019re glad you asked that question. What we\u2019re going to do is search for all the number 1s that happen to be on a line all by themselves. How can we tell if the number 1 is on a line by itself? That\u2019s also easy: if we have a paragraph return followed by the number 1 followed by another paragraph return then we must have a line where the number 1 is sitting there all by its lonesome. (If that doesn\u2019t make sense do this: create a new Word document, press ENTER, type the number 1, then press ENTER again. Voila!) The following line of code lets us specify search <B>Text<\/B> that consists of a paragraph return (using Word\u2019s <B>^p<\/B> syntax) followed by the number 1 followed by another paragraph return:<\/P><PRE class=\"codeSample\">objSelection.Find.Text = &#8220;^p1^p&#8221;\n<\/PRE>\n<P>All we\u2019ve done here is define the text we want to search for. Once that\u2019s done we then set the <B>Forward<\/B> property to True; that tells the script to begin the search at the current selection point and then move forward through the document. Because the selection point is currently at the very beginning of the document (the default location when you create an instance of the Selection object) this ensures that our script searches the entire document from start to finish.<\/P>\n<P>After that we define our replacement text:<\/P><PRE class=\"codeSample\">objSelection.Find.Replacement.Text = &#8220;^p1^p^m&#8221;\n<\/PRE>\n<P>As you no doubt recall, we\u2019re searching for the following bit of text: <B>^p1^p<\/B>. We\u2019re now stating that we want to replace that bit of text with this: <B>^p1^p^m<\/B>. And yes, you\u2019re absolutely right: the <B>^m<\/B> is Word\u2019s syntax for a manual page break. The net effect here is that we\u2019re adding a page break after each line in the file where the number 1 appears by itself. And that\u2019s good; after all, that\u2019s exactly what we <I>want<\/I> to do.<\/P>\n<P>All that\u2019s left now is to call the <B>Execute<\/B> method and actually perform the search-and-replace:<\/P><PRE class=\"codeSample\">objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll\n<\/PRE>\n<P>We won\u2019t discuss all the empty parameters required for this operation; for details see our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/may05\/tips0512.mspx\"><B>Office Space article<\/B><\/A> on finding and replacing text in a Word document. For now just note that all the commas are required; that\u2019s to ensure that the constant wdReplaceAll is passed in the correct position. In addition, the constant wdReplaceAll is required to ensure that <I>all<\/I> instances of the target text get replaced; without specifying this value the script will replace the first instance of the target text and then call it good.<\/P>\n<P>And that would <I>not<\/I> be good.<\/P>\n<P>That should do it, JR. Give this baby a try and see what happens.<\/P>\n<P>As for the Scripting Guy who writes this column, he\u2019s busy rethinking some of his opinions on fashion. For example, after seeing a guy wearing short shorts and cowboy boots he\u2019s beginning to think that maybe guys wearing sandals isn\u2019t such a bad thing after all. <\/P>\n<P>That said, however, he still firmly and resolutely believes that \u2013 what\u2019s that? You just remembered that you left a pot of water boiling on the stove? Well, OK; you better go take care of that. We\u2019ll hold this thought until you get back.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I open a text file and insert a page break after each line where the only character is the number 1?&#8212; JR Hey, JR. OK, to begin with, there\u2019s no doubt that the Scripting Guy who writes this column doesn\u2019t keep up with the world of fashion; for example, he [&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-64483","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 open a text file and insert a page break after each line where the only character is the number 1?&#8212; JR Hey, JR. OK, to begin with, there\u2019s no doubt that the Scripting Guy who writes this column doesn\u2019t keep up with the world of fashion; for example, he [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64483","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=64483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64483\/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=64483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}