«

»

Apr 01 2008

Add two Blank Lines between each Line in a File

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 fso = CreateObject( "Scripting.FileSystemObject" )
Set textFile = fso.OpenTextFile( "c:\scripts\namelist.txt", ForReading )

contents = textFile.ReadAll()
textFile.Close

oldText = vbCrLf
newText = vbCrLf & vbCrLf & vbCrLf

contents = Replace( contents, oldText, newText )
Set textFile = fso.OpenTextFile( "c:\scripts\namelist.txt", ForWriting )
textFile.Write contents
textFile.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 contents, and then close the file.

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 contents 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.

That’s the purpose of the next two lines of code. There we assign values to two variables: oldText, which gets the value of vbCrLf, equivalent to pressing ENTER once; and newText, which gets assigned the value of three vbCrLf’s, equivalent to pressing ENTER three times.

contents = Replace( contents, oldText, newText )

We are using the VBScript Replace function to search through the variable contents. 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 have modified contents we then reopen our text file – this time for writing – and use the Write method to replace the existing contents with our new information.

Based on “How Can I Add Two Blank Lines Between Each Line in a Text File?” from the Scripting Guys.

Comments are closed.