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

Closed
BrianGreen Posts 1005 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 23364 Registration date Sunday June 8, 2008 Status Contributor Last seen December 3, 2024 - 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

into
<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>:
@ECHO OFF

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(
ECHO(!L!
ENDLOCAL
)
) >%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)
Related:

1 response

yg_be Posts 23364 Registration date Sunday June 8, 2008 Status Contributor Last seen December 3, 2024 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 ""
Wend
f1.Close 'close the input file
F2.Close
0