{"id":64423,"date":"2007-07-19T00:52:00","date_gmt":"2007-07-19T00:52:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/19\/how-can-i-add-an-html-tag-to-the-end-of-each-line-in-a-file\/"},"modified":"2007-07-19T00:52:00","modified_gmt":"2007-07-19T00:52:00","slug":"how-can-i-add-an-html-tag-to-the-end-of-each-line-in-a-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-an-html-tag-to-the-end-of-each-line-in-a-file\/","title":{"rendered":"How Can I Add an HTML Tag to the End of Each Line in a 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! I have a bunch of documents I\u2019m trying to transfer to MySpace. In order to do so, I need to put the <B>&lt;br&gt;<\/B> 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?<BR><BR>&#8212; MM <\/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, 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\u2019t necessarily <I>like<\/I> spam, he does find it interesting, and he often reads those unsolicited emails before deleting them. (The emails he deletes <I>without<\/I> reading them tend to be messages sent by his fellow Microsoft employees.)<\/P>\n<P>Today\u2019s email brought him a particularly intriguing piece of spam. \u201cAre you experiencing mail performance problems?\u201d it asked. \u201cIf so, we can help. Try our generic-quality natural performance enhancers.\u201d<\/P>\n<P>Needless to say, there\u2019s 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 \u201cgeneric quality.\u201d That sounds like something he could strive for in his own work.<\/P>\n<P>Even better is the fact that these performance enhancers are for <I>mail<\/I> performance problems. Needless to say, those babies would come in handy. For example, sometimes the Scripting Guy who writes this column doesn\u2019t get his copy of <I>Sports Illustrated<\/I> 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 Amazon.com before you\u2019ve even submitted the order? Wouldn\u2019t <I>that<\/I> be cool?<\/P>\n<TABLE class=\"dataTable\" id=\"ESD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Medical Disclaimer<\/B>. 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.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, eventually the Scripting Guy who writes this column figured out that these were supposed to be performance enhancers for <I>male<\/I> performance problems. Does the Scripting Guy who writes this column suffer from male performance problems? Good question; it\u2019s been so long since anyone has asked him to perform that he\u2019s forgotten.<\/P>\n<P>So we\u2019ll just say \u201cNo\u201d and let it go at that.<\/P>\n<P>Fortunately, the Scripting Guy who writes this column <I>hasn\u2019t<\/I> forgotten how to write a script that can add a &lt;br&gt; tag to the end of every line in a file:<\/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>strContents = objFile.ReadAll\nobjFile.Close\nstrContents = Replace(strContents, vbCrlf, &#8220;&lt;br&gt;&#8221; &amp; vbCrLf)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)<\/p>\n<p>objFile.Write strContents\nobjFile.Close\n<\/PRE>\n<P>Let\u2019s 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\u2019ll 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\u2019re 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 &lt;br&gt; tag at the end of each line).<\/P>\n<P>After defining the constants we next create an instance of the <B>Scripting.FileSystemObject<\/B> object, then use the following line of code to open the file C:\\Scripts\\Test.txt for reading:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\n<\/PRE>\n<P>Next we call the <B>ReadAll<\/B> method to read in the entire contents of the file, storing that information in a variable named strContents. We then call the <B>Close<\/B> method and close the file. But don\u2019t despair; we\u2019ll be reopening the file, this time for writing, in a minute or two.<\/P>\n<P>So why <I>did<\/I> we read in the entire contents of the file and then store that information in a variable? That\u2019s easy: we did that because the FileSystemObject doesn\u2019t allow us to directly modify the contents of a file. Instead, we need to read the contents into memory, modify the \u201cvirtual\u201d version of that file, and then overwrite the existing file with these modified contents. We\u2019ve already done part 1 of this 3-part process: we\u2019ve read the contents into memory. Now it\u2019s time for part 2, modifying the virtual version of our file:<\/P><PRE class=\"codeSample\">strContents = Replace(strContents, vbCrlf, &#8220;&lt;br&gt;&#8221; &amp; vbCrLf)\n<\/PRE>\n<P>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\u2019t see that character if you view the file in Notepad, but trust us, it\u2019s there.)<\/P>\n<P>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 <B>vbCrLf<\/B>). That also makes it very easy to add the &lt;br&gt; tag to the end of each line; to do that all we need to do is replace each carriage return-linefeed character with the &lt;br&gt; tag <I>plus<\/I> a carriage return-linefeed character. Suppose our text file looks like this, with the asterisk representing the carriage return-linefeed character:<\/P><PRE class=\"codeSample\">This is line 1.* \nThis is line 2.* \nThis is line 3.*\n<\/PRE>\n<P>If we replace each carriage return-linefeed (in this example, each asterisk) with the &lt;br&gt; tag and a carriage return-linefeed character our text file will end up looking like this:<\/P><PRE class=\"codeSample\">This is line 1.&lt;br&gt;* \nThis is line 2.&lt;br&gt;* \nThis is line 3.&lt;br&gt;*\n<\/PRE>\n<P>Which is good; that\u2019s what we <I>want<\/I> it to look like.<\/P>\n<P>To make a long story short, that\u2019s what we use the <B>Replace<\/B> function for: we search the value of strContents for each instance of vbCrLf, replacing those instances with the &lt;br&gt; tag plus vbCrLf. Just like the line of code suggests:<\/P><PRE class=\"codeSample\">strContents = Replace(strContents, vbCrlf, &#8220;&lt;br&gt;&#8221; &amp; vbCrLf)\n<\/PRE>\n<P>That was easier than you thought it was going to be, wasn\u2019t it?<\/P>\n<P>The next part, part 3, is even easier: we reopen the file C:\\Scripts\\Test.txt, this time for writing, then call the <B>Write<\/B> method to write the revised value of strContents to the file:<\/P><PRE class=\"codeSample\">objFile.Write strContents\n<\/PRE>\n<P>At that point we call the <B>Close<\/B> method to close the file and call it a day. The net result? Each line in the text file now has the &lt;br&gt; tag at the end (again, using an asterisk to represent the invisible carriage return-linefeed character):<\/P><PRE class=\"codeSample\">This is line 1.&lt;br&gt;*\nThis is line 2.&lt;br&gt;*\nThis is line 3.&lt;br&gt;*\n<\/PRE>\n<P>That should do it, MM. Now, if you wouldn\u2019t 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 <I>work<\/I> performance problems? Based on his last performance review that\u2019s something the Scripting Guy who writes this column could <I>really<\/I> use.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a bunch of documents I\u2019m trying to transfer to MySpace. In order to do so, I need to put the &lt;br&gt; 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?&#8212; MM Hey, MM. You [&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-64423","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! I have a bunch of documents I\u2019m trying to transfer to MySpace. In order to do so, I need to put the &lt;br&gt; 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?&#8212; MM Hey, MM. You [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64423","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=64423"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64423\/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=64423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}