How Can I Add an HTML Tag to the End of Each Line in a File?


Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a bunch of documents I’m trying to transfer to MySpace. In order to do so, I need to put the <br> tag at the end of each and every line in the document. Is there any way to do that using Microsoft Word, or even Notepad?

— MM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MM. You know, most people dislike spam. (We mean the unsolicited email, not the lunch meat. The lunch meat is surprisingly good, provided you grill it before eating.) And while the Scripting Guy who writes this column doesn’t necessarily like spam, he does find it interesting, and he often reads those unsolicited emails before deleting them. (The emails he deletes without reading them tend to be messages sent by his fellow Microsoft employees.)

Today’s email brought him a particularly intriguing piece of spam. “Are you experiencing mail performance problems?” it asked. “If so, we can help. Try our generic-quality natural performance enhancers.”

Needless to say, there’s a lot of interesting stuff packed into that little message. Generic-quality natural performance enhancers? To tell you the truth, the Scripting Guy who writes this column has no idea what those could be. But he definitely likes the idea of getting performance enhancers that are of “generic quality.” That sounds like something he could strive for in his own work.

Even better is the fact that these performance enhancers are for mail performance problems. Needless to say, those babies would come in handy. For example, sometimes the Scripting Guy who writes this column doesn’t get his copy of Sports Illustrated until Saturday. With a mail performance enhancer, he might get that same magazine on Thursday, or maybe even on Wednesday. Getting Christmas cards in July? Receiving a shipment from before you’ve even submitted the order? Wouldn’t that be cool?

Medical Disclaimer. If you take any medicines that have nitrates in them you should not use mail performance enhancers, not even those of generic quality. The most common side effects of mail performance enhancers are headache, facial flushing, and upset stomach. If you continue to receive the same magazine for more than four hours at a time please seek immediate medical help.

Of course, eventually the Scripting Guy who writes this column figured out that these were supposed to be performance enhancers for male performance problems. Does the Scripting Guy who writes this column suffer from male performance problems? Good question; it’s been so long since anyone has asked him to perform that he’s forgotten.

So we’ll just say “No” and let it go at that.

Fortunately, the Scripting Guy who writes this column hasn’t forgotten how to write a script that can add a <br> tag to the end of every line in a file:

Const ForReading = 1
Const ForWriting = 2

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

strContents = objFile.ReadAll objFile.Close strContents = Replace(strContents, vbCrlf, “<br>” & vbCrLf)

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

objFile.Write strContents objFile.Close

Let’s see if we can figure out how this script works. As you can see, we start out by defining a pair of constants, ForReading and ForWriting; we’ll use these constants to tell the script whether we want to open our file and read from it (the ForReading constant) or whether we want to open our file and write to it (the ForWriting constant). And, yes, we need both of these constants because we’re going to have to open the file twice: once to read the existing contents, and a second time to write out the modified contents (that is, the file with the <br> tag at the end of each line).

After defining the constants we next create an instance of the Scripting.FileSystemObject object, then use the following line of code to open the file C:\Scripts\Test.txt for reading:

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

Next we call the ReadAll method to read in the entire contents of the file, storing that information in a variable named strContents. We then call the Close method and close the file. But don’t despair; we’ll be reopening the file, this time for writing, in a minute or two.

So why did we read in the entire contents of the file and then store that information in a variable? That’s easy: we did that because the FileSystemObject doesn’t allow us to directly modify the contents of a file. Instead, we need to read the contents into memory, modify the “virtual” version of that file, and then overwrite the existing file with these modified contents. We’ve already done part 1 of this 3-part process: we’ve read the contents into memory. Now it’s time for part 2, modifying the virtual version of our file:

strContents = Replace(strContents, vbCrlf, “<br>” & vbCrLf)

Our original file, C:\Scripts\Test.txt, is just a regular old text file: nothing but a bunch of lines of text. How do you delineate a line in a text file? You know the answer to that one: you just keep typing along until you want to start a new line; at that point, you simply press the ENTER key on the keyboard. In turn, that causes a carriage return-linefeed character to be added to the end of each line. (You won’t see that character if you view the file in Notepad, but trust us, it’s there.)

The important thing is this: we can determine the end of each line simply by looking for instances of the carriage return-linefeed character (represented by the VBScript constant vbCrLf). That also makes it very easy to add the <br> tag to the end of each line; to do that all we need to do is replace each carriage return-linefeed character with the <br> tag plus a carriage return-linefeed character. Suppose our text file looks like this, with the asterisk representing the carriage return-linefeed character:

This is line 1.* 
This is line 2.* 
This is line 3.*

If we replace each carriage return-linefeed (in this example, each asterisk) with the <br> tag and a carriage return-linefeed character our text file will end up looking like this:

This is line 1.<br>* 
This is line 2.<br>* 
This is line 3.<br>*

Which is good; that’s what we want it to look like.

To make a long story short, that’s what we use the Replace function for: we search the value of strContents for each instance of vbCrLf, replacing those instances with the <br> tag plus vbCrLf. Just like the line of code suggests:

strContents = Replace(strContents, vbCrlf, “<br>” & vbCrLf)

That was easier than you thought it was going to be, wasn’t it?

The next part, part 3, is even easier: we reopen the file C:\Scripts\Test.txt, this time for writing, then call the Write method to write the revised value of strContents to the file:

objFile.Write strContents

At that point we call the Close method to close the file and call it a day. The net result? Each line in the text file now has the <br> tag at the end (again, using an asterisk to represent the invisible carriage return-linefeed character):

This is line 1.<br>*
This is line 2.<br>*
This is line 3.<br>*

That should do it, MM. Now, if you wouldn’t mind, could you do us a favor in return: could you check your spam mail every now and then and see if anyone is selling generic-quality natural performance enhancers for work performance problems? Based on his last performance review that’s something the Scripting Guy who writes this column could really use.


Discussion is closed.

Feedback usabilla icon