{"id":69963,"date":"2005-04-22T16:28:00","date_gmt":"2005-04-22T16:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/22\/how-can-i-add-two-blank-lines-between-each-line-in-a-text-file\/"},"modified":"2005-04-22T16:28:00","modified_gmt":"2005-04-22T16:28:00","slug":"how-can-i-add-two-blank-lines-between-each-line-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-two-blank-lines-between-each-line-in-a-text-file\/","title":{"rendered":"How Can I Add Two Blank Lines Between Each Line 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! Hey, Scripting Guy! How can I add two blank lines between each line in a text file?<BR><BR>&#8212; LW<\/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, LW. You know, when we wrote the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/default.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A> we included 4 or 5 examples of how you might work with text files. And then we thought to ourselves, \u201cWell, we\u2019ve pretty much exhausted all the things you can do with text files. We\u2019ll never be called upon to answer a text file question ever again.\u201d <\/P>\n<P>OK, so we got <I>that<\/I> wrong. As it is, 1 out of every 10 or so questions sent to us deals with text files; we get 3 or 4 text file-related questions every day. And most of them deal with situations we never even thought about, situations just like this: how can I add two blank lines between each line in a text file?<\/P>\n<P>Well, let\u2019s see what we can do about that. Suppose we have a text file &#8211; left over from a previous column &#8211; that consists of a bunch of names:<\/P><PRE class=\"codeSample\">Ken Myer\nPilar Ackerman\nJonathan Haas\nSyed Abbas\n<\/PRE>\n<P>Is there an easy way to put two blank lines between each of these names? You bet there is:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;c:\\scripts\\namelist.txt&#8221;, ForReading)<\/p>\n<p>strContents = objFile.ReadAll()\nobjFile.Close<\/p>\n<p>strOldText = vbCrLf\nstrNewText = vbCrLf &amp; vbCrLf &amp; vbCrLf<\/p>\n<p>strContents = Replace(strContents, strOldText, strNewText)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;c:\\scripts\\namelist.txt&#8221;, ForWriting)\nobjFile.Write strContents\nobjFile.Close\n<\/PRE>\n<P>We begin by defining a pair of constants &#8211; ForReading and ForWriting &#8211; that we\u2019ll use when working with our text file. We create an instance of the <B>FileSystemObject<\/B> and use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Namelist.txt. We use the <B>ReadAll()<\/B> method to read the entire contents of the file into a variable named strContents, and then close the file.<\/P>\n<P>As you know, when you create a text file such as our list of names you type some information on line 1, press ENTER, and type something on line 2. Each time you want to start a new line you press ENTER, an action which &#8211; on a more technical level &#8211; inserts a carriage return-linefeed character into the file. You won\u2019t actually see this character, but it\u2019s there, and VBScript knows it; in fact, VBScript has a built-in constant &#8211; <B>vbCrLf<\/B> &#8211; that maps to the carriage return-linefeed.<\/P>\n<P>Why do we care about that? Well, how would you put two blank lines between each line in a text file? If you\u2019re doing this manually, you\u2019d type in line 1, then press ENTER three times. Press ENTER once and line 2 falls directly under line 1. Press ENTER three times and you\u2019ll end up with line 1, two blank lines, and <I>then<\/I> line 2. What does that mean? Well, our variable strContents includes carriage return-linefeeds. If we can replace each of those carriage return-linefeed characters with <I>three<\/I> such characters we\u2019ll end up with two blank lines between each line in the text file. (Try to visualize that and you\u2019ll see what we\u2019re talking about.)<\/P>\n<P>That\u2019s the purpose of the next two lines of code. There we assign values to two variables: strOldText, which gets the value of vbCrLf, equivalent to pressing ENTER once; and strNewText, which gets assigned the value of <I>three<\/I> vbCrLf\u2019s, equivalent to pressing ENTER three times.<\/P>\n<P>All that is a prelude to this line of code:<\/P><PRE class=\"codeSample\">strContents = Replace(strContents, strOldText, strNewText)\n<\/PRE>\n<P>What\u2019s going on here? Well, we\u2019re using the VBScript <B>Replace<\/B> function to search through the variable strContents (which, as you recall, contains the contents of our text file). The Replace function will locate each carriage return-linefeed character and replace it with three such characters; that\u2019s going to give us two blank lines between each line in the file. After we\u2019ve modified strContents we then reopen our text file &#8211; this time for writing &#8211; and use the <B>Write<\/B> method to replace the existing contents with our new information. If we run the script and then open the text file, it\u2019s going to look just like this:<\/P><PRE class=\"codeSample\">Ken Myer<\/p>\n<p>Pilar Ackerman<\/p>\n<p>Jonathan Haas<\/p>\n<p>Syed Abbas\n<\/PRE>\n<P>Now if that isn\u2019t everything you could ever want to know about text files &#8211; uh, never mind. Never again will we underestimate your ability to come up with new and interesting uses for the good old-fashioned text file. Cross our hearts!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Hey, Scripting Guy! How can I add two blank lines between each line in a text file?&#8212; LW Hey, LW. You know, when we wrote the Microsoft Windows 2000 Scripting Guide we included 4 or 5 examples of how you might work with text files. And then we thought to ourselves, \u201cWell, [&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-69963","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! Hey, Scripting Guy! How can I add two blank lines between each line in a text file?&#8212; LW Hey, LW. You know, when we wrote the Microsoft Windows 2000 Scripting Guide we included 4 or 5 examples of how you might work with text files. And then we thought to ourselves, \u201cWell, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69963","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=69963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69963\/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=69963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}