Creating a batch/vbs file to append start and end of paragraph

BrianGreen Posts 1010 Registration date Saturday January 17, 2015 Status Moderator Last seen September 30, 2021 - Updated by BrianGreen on 8/12/16 at 08:34 AM
yg_be Posts 21820 Registration date Sunday June 8, 2008 Status Contributor Last seen October 3, 2023 - Jan 2, 2017 at 10:12 AM
Hi All,

Anyone want a free virtual beer before the Christmas season starts?

I cant believe how hard I am finding this. What I want to do is (in essence) create a web page from a text file that someone at work has written. At this stage all I want to do is add <p> and </p> to the start and end of every paragraph and for this to be separated with a blank line. So what I want to do is convert this
Text 1
Text 2
Text 3

<p>Text 1</p>

<p>Text 2</p>

<p>Text 3</p>

Simple? I failed dismally, but have a bit of success with this in the following batch file, but its not quite what I want - I would need to edit this a bit to get it to look nice and to remove the first </p> and add a final </p>:

rem **************************
rem ** Set Variables **
rem **************************

SET origfile="C:\Users\Brian\Desktop\Untitled 1.txt"
SET tempfile="C:\Users\Brian\Desktop\Untitled 1 copy.txt"
SET insertbefore=""

rem **************************
rem ** Find number of lines **
rem **************************

FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C

rem **************************
rem * Print the new text and save the new file *
rem **************************

<%origfile% (FOR /L %%i IN (1,1,%totallines%) DO (
SETLOCAL EnableDelayedExpansion
SET /P L="</p><p>"
IF %%i==%insertbefore% ECHO(
) >%tempfile%

What this produces is
</p><p>Text 1
</p><p>Text 2
</p><p>Text 3

I hope you can help a weary old men and I will let all contributers all into my virtual pub for beers and a mince pie (it is nearly Christmas!).

I really appreciate thank you messages as a payment for solving issues   :o)

1 response

yg_be Posts 21820 Registration date Sunday June 8, 2008 Status Contributor Last seen October 3, 2023 5
Jan 2, 2017 at 10:12 AM
Hi, I am proposing this vbs:
Dim textline, fso, f1, F2, aline
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.OpenTextFile("C:\data\temp\ccm\input.txt", 1, True)
Set F2 = fso.CreateTextFile("C:\data\temp\ccm\output.txt")
While f1.AtEndOfStream <> True
        aline = f1.Readline        'read in the row
        F2.WriteLine "<p>" & aline & "</p>"
        F2.WriteLine ""
f1.Close 'close the input file