How Can I Add Two Blank Lines Between Each Line in a Text File?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Hey, Scripting Guy! How can I add two blank lines between each line in a text file?

— LW

SpacerHey, Scripting Guy! AnswerScript Center

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, “Well, we’ve pretty much exhausted all the things you can do with text files. We’ll never be called upon to answer a text file question ever again.”

OK, so we got that 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?

Well, let’s see what we can do about that. Suppose we have a text file – left over from a previous column – that consists of a bunch of names:

Ken Myer
Pilar Ackerman
Jonathan Haas
Syed Abbas

Is there an easy way to put two blank lines between each of these names? You bet there is:

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objFile = objFSO.OpenTextFile(“c:\scripts\namelist.txt”, ForReading)

strContents = objFile.ReadAll() objFile.Close

strOldText = vbCrLf strNewText = vbCrLf & vbCrLf & vbCrLf

strContents = Replace(strContents, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(“c:\scripts\namelist.txt”, ForWriting) objFile.Write strContents objFile.Close

We begin by defining a pair of constants – ForReading and ForWriting – that we’ll use when working with our text file. We create an instance of the FileSystemObject and use the OpenTextFile method to open the file C:\Scripts\Namelist.txt. We use the ReadAll() method to read the entire contents of the file into a variable named strContents, and then close the file.

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 – on a more technical level – inserts a carriage return-linefeed character into the file. You won’t actually see this character, but it’s there, and VBScript knows it; in fact, VBScript has a built-in constant – vbCrLf – that maps to the carriage return-linefeed.

Why do we care about that? Well, how would you put two blank lines between each line in a text file? If you’re doing this manually, you’d 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’ll end up with line 1, two blank lines, and then 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 three such characters we’ll end up with two blank lines between each line in the text file. (Try to visualize that and you’ll see what we’re talking about.)

That’s 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 three vbCrLf’s, equivalent to pressing ENTER three times.

All that is a prelude to this line of code:

strContents = Replace(strContents, strOldText, strNewText)

What’s going on here? Well, we’re using the VBScript Replace 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’s going to give us two blank lines between each line in the file. After we’ve modified strContents we then reopen our text file – this time for writing – and use the Write method to replace the existing contents with our new information. If we run the script and then open the text file, it’s going to look just like this:

Ken Myer

Pilar Ackerman

Jonathan Haas

Syed Abbas

Now if that isn’t everything you could ever want to know about text files – 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!

0 comments

Discussion is closed.

Feedback usabilla icon