How Can I Remove All the Blank Lines from a Text File?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I remove all the blank lines from a text file?

— RE

SpacerHey, Scripting Guy! AnswerScript Center

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 people who thought it was a mistake to even mention text files. Why? Because nobody uses text files any more, and it was silly for us to waste our time on a dead technology.







Note. 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’s another story.



What’s the moral to all this? It’s now 2006, and the first question being tackled in the Hey, Scripting Guy! column is one dealing with text files. Not bad for a dead technology that nobody uses, huh?


We’re assuming that you have a text file that looks something like this:

Line 1

Line 2

Line 3


What you’d like is for the text file to look like this:

Line 1
Line 2
Line 3

Can you do that using a script? Of course you can:

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(“C:\Scripts\Test.txt”, ForReading)

Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = Trim(strLine)
If Len(strLine) > 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile(“C:\Scripts\Test.txt”, ForWriting)
objFile.Write strNewContents
objFile.Close


Let’s 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’s script is no exception. Because we can’t 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’s not, we add that line (along with any other non-blank lines) to a variable named strNewContents. When we’re 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’t 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.


Got all that? OK. Now let’s walk through the script step-by-step.


We start off by defining a pair of constants named ForReading and ForWriting; we’ll 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 Scripting.FileSystemObject and then open the file C:\Scripts\Test.txt for reading:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(“C:\Scripts\Test.txt”, ForReading)

Next we have this block of code:

Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = Trim(strLine)
If Len(strLine) > 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop

What we’re doing here is reading the file line-by-line, starting from the beginning and ending, well, at the end. (How do we know we’ve reached the end of the file? When the AtEndOfStream property is True.)


Inside this loop we use the ReadLine 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 – and the Trim function – to remove any blank spaces from the beginning and/or the end of the line:

strLine = Trim(strLine)

Why do we do that? Well, suppose you have a bunch of lines that consist of a single blank space. We’re assuming that you’re counting those as blank lines and want them deleted. If you don’t want those lines deleted, then just remove this line of code from the script:

strLine = Trim(strLine)

That brings us to here:

If Len(strLine) > 0 Then

What we’re doing now is using the Len function to determine the number of characters in the line. If Len is greater than 0 then this is not a blank line; consequently, we add the line (plus a carriage return-linefeed) to a variable named strNewContents:

strNewContents = strNewContents & strLine & vbCrLf

And yes, we add this line to whatever currently happens to be in the variable strNewContents. That’s why we concatenate the existing value of strNewContents and the variable strLine and a carriage return-linefeed (using the VBScript constant vbCrLf).


But what if Len is not greater than 0? If that’s the case then we have a blank line and we don’t add the line to strNewContents. We then loop around and repeat the process for the next line in the text file.


After closing the file C:\Scripts\Test.txt we then reopen it (this time for writing) and call the Write method to write the value of strNewContents to the file. Why? That’s easy: because strNewContents consists of all the lines in the original text file except for the blank lines; those were never added to strNewContents. So where does that leave us? You guessed it:

Line 1
Line 2
Line 3

A little cumbersome, but, like we said, not bad for a “dead” technology.