Read Text file, entire text and display it to HTML

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Read Text file, entire text and display it to HTML

#1 Post by booga73 » 06 Jan 2013 13:11

I have this batch file code that I wrote. I would like to read the contents of a text file with all the formatting of each line and convert it to HTML.

I have the part for conversion of the html good, but the batch file to read each line isn't working.
I would like to maintain all the original text in the text file as is, but simply read the entire text file from the for loop and echo each line / text in each line out to an html file for easy display.

my current batch file is:

:: start of batch file
@ECHO OFF

FOR /F "eol=; tokens=*" %%i IN ("C:\irtXMT\irtmwb\irtscanlog.txt\irtscanLog.txt") do echo ^<table border="1" BORDERCOLOR="#000000" CELLPADDING="1" CELLSPACING="1" ^> ^<tr^> ^<td width="80" Align="Left" style="background-color:#89A9FF; font:8pt Arial;" ^> %%o ^</td^> ^</tr^> ^</table^> >>c:\irtXMT\iRTMWB.htm

CD\
cd irtXMT
start irtmwb.htm

pause
:: end of batch file


now, though the output of my .htm file / the HTML display shows fine for now, but the out put of the text to be read shows simply: %0
What I would like help with is to read the entire contents of the log file, irtscanlog.txt per each line as is and output the log file to html.
Can I get some assistance, please?

this is the sample of the irtscanlog.txt:

Scan type: Quick scan
Scan options enabled: Memory | Startup | Registry | File System | Heuristics/Extra | Heuristics/Shuriken | PUP | PUM
Scan options disabled: P2P
Objects scanned: 227653
Time elapsed: 1 minute(s), 45 second(s)

Memory Processes Detected: 0
(No malicious items detected)

Memory Modules Detected: 0
(No malicious items detected)

Registry Keys Detected: 0
(No malicious items detected)

Registry Values Detected: 0
(No malicious items detected)

Registry Data Items Detected: 0
(No malicious items detected)

Folders Detected: 0
(No malicious items detected)

Files Detected: 0
(No malicious items detected)

(end)

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Read Text file, entire text and display it to HTML

#2 Post by Aacini » 06 Jan 2013 13:44

The FOR replaceable parameter placed before ^</td^> must be %%i instead %%o:

Code: Select all

:: start of batch file
@ECHO OFF

FOR /F "eol=; tokens=*" %%i IN ("C:\irtXMT\irtmwb\irtscanlog.txt\irtscanLog.txt") do echo ^<table border="1" BORDERCOLOR="#000000" CELLPADDING="1" CELLSPACING="1" ^> ^<tr^> ^<td width="80" Align="Left" style="background-color:#89A9FF; font:8pt Arial;" ^> %%i ^</td^> ^</tr^> ^</table^> >>c:\irtXMT\iRTMWB.htm

CD\
cd irtXMT
start irtmwb.htm

pause
:: end of batch file


I suggest you to use "delims=" instead of "tokens=*", unless you want to eliminate leading/trailing spaces.

Antonio

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Read Text file, entire text and display it to HTML

#3 Post by booga73 » 06 Jan 2013 13:52

Antonio,

Sir you are incredible, thank you for your time Sir. I apprieciate your help.

you know, I was doing additional research too and I agree with your coding and your suggestion about the use of %%0; I should be using %%i.

after digging further and I also found this too,

setlocal enabledelayedexpansion

FOR /F "tokens=* delims=*" %%i IN (C:\irtXMT\irtmwb\irtscanlog.txt\irtscan.txt) do (

echo ^<table border="1" BORDERCOLOR="#000000" CELLPADDING="1" CELLSPACING="1" ^> ^<tr^> ^<td width="850" Align="Left" style="background-color:#89A9FF; font:8pt Arial;" ^> %%i ^</td^> ^</tr^> ^</table^> >>c:\irtXMT\iRTMWB.htm
endlocal
)

this works too; thank you again, have a Great Weekend Sir!

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Read Text file, entire text and display it to HTML

#4 Post by Squashman » 06 Jan 2013 16:29

You have to use %%i because that is your loop variable. What made you think you could use %%o?

You do not want to use "delims=*". You want to use what Aacini told you to use.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Read Text file, entire text and display it to HTML

#5 Post by Ed Dyreen » 06 Jan 2013 17:53

booga73 wrote:Antonio,

Sir you are incredible, thank you for your time Sir. I apprieciate your help.

you know, I was doing additional research too and I agree with your coding and your suggestion about the use of %%0; I should be using %%i.

after digging further and I also found this too,

setlocal enabledelayedexpansion

FOR /F "tokens=* delims=*" %%i IN (C:\irtXMT\irtmwb\irtscanlog.txt\irtscan.txt) do (

echo ^<table border="1" BORDERCOLOR="#000000" CELLPADDING="1" CELLSPACING="1" ^> ^<tr^> ^<td width="850" Align="Left" style="background-color:#89A9FF; font:8pt Arial;" ^> %%i ^</td^> ^</tr^> ^</table^> >>c:\irtXMT\iRTMWB.htm
endlocal
)

this works too; thank you again, have a Great Weekend Sir!
To option 'tokens' the '*' char has special meaning; return all tokens. But option 'delims' takes it literaly; split on every '*' char.

Code: Select all

>for /f "delims=" %? in ("o hell*o") do echo.%?

>echo.o hell*o
o hell*o

>for /f "delims=*" %? in ("o hell*o") do echo.%?

>echo.o hell
o hell

>
If you want the entire line, undelimited, use;

Code: Select all

>for /f delims^=^ eol^= %? in ("o hell*o") do echo.%?

>echo.o hell*o
o hell*o

>
But redirecting to %%i works, cause '*' is not a valid char for fileNames and ';' is apart from linefeed the default EndOfLine char :D

Code: Select all

for /f delims^=^ eol^= %%i in (C:\irtXMT\irtmwb\irtscanlog.txt\irtscan.txt) do (...
:roll: :wink: ed

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Read Text file, entire text and display it to HTML

#6 Post by booga73 » 06 Jan 2013 19:38

Squashman,

I happened to copy / paste some code from my other work to not "reinvent" code and I litterally forgot the %%0 was still in place which I should have used %%i.

I also, through recommendations removed the "delims=*" parameter from my code.

Thank you Guys - Aacini, Squashman and Ed,.

best regards,
Booga73

Post Reply