{"id":68263,"date":"2006-01-03T18:10:00","date_gmt":"2006-01-03T18:10:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/03\/how-can-i-remove-all-the-blank-lines-from-a-text-file\/"},"modified":"2006-01-03T18:10:00","modified_gmt":"2006-01-03T18:10:00","slug":"how-can-i-remove-all-the-blank-lines-from-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-remove-all-the-blank-lines-from-a-text-file\/","title":{"rendered":"How Can I Remove All the Blank Lines from a Text File?"},"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 remove all the blank lines from a text file?<BR><BR>&#8212; RE<\/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, RE. You know, back in the year 2002, when the Scripting Guys were still just lowercase scripting guys, we began working on the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/default.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>. In putting together the book we faced enormous resistance from people who thought it was a mistake to even <I>mention<\/I> text files. Why? Because nobody uses text files any more, and it was silly for us to waste our time on a dead technology.<\/P>\n<TABLE class=\"dataTable\" id=\"ECD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Believe it or not, we also faced enormous resistance to including a chapter on VBScript, even though this was a book designed to teach people how to write scripts using VBScript. But that\u2019s another story.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>What\u2019s the moral to all this? It\u2019s now 2006, and the first question being tackled in the <I>Hey, Scripting Guy!<\/I> column is one dealing with text files. Not bad for a dead technology that nobody uses, huh?<\/P>\n<P>We\u2019re assuming that you have a text file that looks something like this:<\/P><PRE class=\"codeSample\">Line 1<\/p>\n<p>Line 2<\/p>\n<p>Line 3\n<\/PRE>\n<P>What you\u2019d <I>like<\/I> is for the text file to look like this:<\/P><PRE class=\"codeSample\">Line 1\nLine 2\nLine 3\n<\/PRE>\n<P>Can you do that using a script? Of course you can:<\/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\\Test.txt&#8221;, ForReading)<\/p>\n<p>Do Until objFile.AtEndOfStream\n    strLine = objFile.Readline\n    strLine = Trim(strLine)\n    If Len(strLine) &gt; 0 Then\n        strNewContents = strNewContents &amp; strLine &amp; vbCrLf\n    End If\nLoop<\/p>\n<p>objFile.Close<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\nobjFile.Write strNewContents\nobjFile.Close\n<\/PRE>\n<P>Let\u2019s talk about the basic idea before we get into the nitty-gritty details of how the script works. As you probably know, manipulating text files often involves coming up with work-arounds. Today\u2019s script is no exception. Because we can\u2019t directly edit a text file we have to do this instead: First, we read in the text file line-by-line. As we read each line we check to see if the line is blank. If it is, we discard it; if it\u2019s not, we add that line (along with any other non-blank lines) to a variable named strNewContents. When we\u2019re done reading the file we close it, then immediately reopen it for writing. (One of the vagaries of working with text files: you can read from a file or you can write to a file, but you can\u2019t do both at the same time.) We then write the value of strNewContents to the file. The net result: we end up with a file that has no blank lines in it.<\/P>\n<P>Got all that? OK. Now let\u2019s walk through the script step-by-step.<\/P>\n<P>We start off by defining a pair of constants named ForReading and ForWriting; we\u2019ll use these constants to specify the appropriate mode when opening our text file. We then use these two lines of code to create an instance of the <B>Scripting.FileSystemObject<\/B> and then open the file C:\\Scripts\\Test.txt for reading:<\/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>Next we have this block of code:<\/P><PRE class=\"codeSample\">Do Until objFile.AtEndOfStream\n    strLine = objFile.Readline\n    strLine = Trim(strLine)\n    If Len(strLine) &gt; 0 Then\n        strNewContents = strNewContents &amp; strLine &amp; vbCrLf\n    End If\nLoop\n<\/PRE>\n<P>What we\u2019re doing here is reading the file line-by-line, starting from the beginning and ending, well, at the end. (How do we know we\u2019ve reached the end of the file? When the <B>AtEndOfStream<\/B> property is True.) <\/P>\n<P>Inside this loop we use the <B>ReadLine<\/B> method to read the first line of the text file and store it in a variable named strLine. We then use this line of code &#8211; and the <B>Trim<\/B> function &#8211; to remove any blank spaces from the beginning and\/or the end of the line:<\/P><PRE class=\"codeSample\">strLine = Trim(strLine)\n<\/PRE>\n<P>Why do we do that? Well, suppose you have a bunch of lines that consist of a single blank space. We\u2019re assuming that you\u2019re counting those as blank lines and want them deleted. If you don\u2019t want those lines deleted, then just remove this line of code from the script:<\/P><PRE class=\"codeSample\">strLine = Trim(strLine)\n<\/PRE>\n<P>That brings us to here:<\/P><PRE class=\"codeSample\">If Len(strLine) &gt; 0 Then\n<\/PRE>\n<P>What we\u2019re doing now is using the <B>Len<\/B> function to determine the number of characters in the line. If Len is greater than 0 then this is <I>not<\/I> a blank line; consequently, we add the line (plus a carriage return-linefeed) to a variable named strNewContents:<\/P><PRE class=\"codeSample\">strNewContents = strNewContents &amp; strLine &amp; vbCrLf\n<\/PRE>\n<P>And yes, we <I>add<\/I> this line to whatever currently happens to be in the variable strNewContents. That\u2019s why we concatenate the existing value of strNewContents <I>and<\/I> the variable strLine <I>and<\/I> a carriage return-linefeed (using the VBScript constant vbCrLf).<\/P>\n<P>But what if Len is <I>not<\/I> greater than 0? If that\u2019s the case then we have a blank line and we <I>don\u2019t<\/I> add the line to strNewContents. We then loop around and repeat the process for the next line in the text file.<\/P>\n<P>After closing the file C:\\Scripts\\Test.txt we then reopen it (this time for writing) and call the <B>Write<\/B> method to write the value of strNewContents to the file. Why? That\u2019s easy: because strNewContents consists of all the lines in the original text file <I>except for<\/I> the blank lines; those were never added to strNewContents. So where does that leave us? You guessed it:<\/P><PRE class=\"codeSample\">Line 1\nLine 2\nLine 3\n<\/PRE>\n<P>A little cumbersome, but, like we said, not bad for a \u201cdead\u201d technology.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I remove all the blank lines from a text file?&#8212; RE Hey, RE. You know, back in the year 2002, when the Scripting Guys were still just lowercase scripting guys, we began working on the Microsoft Windows 2000 Scripting Guide. In putting together the book we faced enormous resistance from [&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-68263","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 remove all the blank lines from a text file?&#8212; RE Hey, RE. You know, back in the year 2002, when the Scripting Guys were still just lowercase scripting guys, we began working on the Microsoft Windows 2000 Scripting Guide. In putting together the book we faced enormous resistance from [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68263","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=68263"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68263\/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=68263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}