Creating a custom html generator in batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hacxx
Posts: 57
Joined: 09 Apr 2015 13:18

Creating a custom html generator in batch

#1 Post by hacxx » 20 Oct 2020 04:31

Hi,

I'm looking for more info on how to setup this properly...
I want to create a batch that will prompt the user for input and generate html files in the end.

The html code is below

Code: Select all

echo "<iframe src='myfile.html' frameborder='0' height='100%' width='100%'></iframe>" >index.html
echo "myfile.html here" >myfile.html
The problem is if i use " after echo this will be printed in the output file.
If i remove " the code is invalid because of <.
I was thinking in using \x3c but this gets printed as is on the file.

Thanks for all the help provided
Here is my information repository - http://www.cyberlord.at/forum/?id=10589

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Creating a custom html generator in batch

#2 Post by OJBakker » 20 Oct 2020 06:17

Code: Select all

echo "<iframe src='myfile.html' frameborder='0' height='100%' width='100%'></iframe>" > index.html
echo "myfile.html here" >myfile.html

rem set variables with set "var=..." to get rid of the double quots.
set "var1=<iframe src='myfile.html' frameborder='0' height='100%' width='100%'></iframe>"
set "var2=myfile.html here"
echo %var2%>myfile.html

rem for normal echo escape the special characters
set "var1A=%var1%"
set "var1A=%var1A:<=^<%"
set "var1A=%var1A:>=^>%"
echo %var1A%> indexA.html

rem echo using the set/p prompt string, no need to escape special characters,
rem but an additional echo is needed to add the CRLF at the end of the line.
<nul (set/p "=%var1%"&(echo:))> indexB.html

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating a custom html generator in batch

#3 Post by aGerman » 20 Oct 2020 10:11

Another possibility is to use a subenvironment with delayed expansion enabled for the redirection.

Code: Select all

set "var=<iframe src='myfile.html' frameborder='0' height='100%' width='100%'></iframe>"
setlocal EnableDelayedExpansion
>"index.html" echo(!var!
endlocal
However, while this works around the redirection of angle brackets and other characters with special meaning in Batch, it doesn't necessarily result in valid HTML. Consider to change the codepage to UTF-8 using CHCP 65001. And probably you have to ask the user to enter HTML entities for special characters because you won't be able to replace them in the code. Think about something like <b>5>2</b>. Only the > in the middle has to be replaced with &gt;.

Steffen

hacxx
Posts: 57
Joined: 09 Apr 2015 13:18

Re: Creating a custom html generator in batch

#4 Post by hacxx » 20 Oct 2020 13:04

aGerman wrote:
20 Oct 2020 10:11
However, while this works around the redirection of angle brackets and other characters with special meaning in Batch, it doesn't necessarily result in valid HTML. Consider to change the codepage to UTF-8 using CHCP 65001. And probably you have to ask the user to enter HTML entities for special characters because you won't be able to replace them in the code. Think about something like <b>5>2</b>. Only the > in the middle has to be replaced with &gt;.
Yes i'm having a issue that i didn't notice in the begining, how do i write into file the caracter "?
(" is been used by echo and cannot be used inside the text...)

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating a custom html generator in batch

#5 Post by aGerman » 20 Oct 2020 13:32

In what context did you face an issue?

Code: Select all

>"test.txt" echo "
Steffen

hacxx
Posts: 57
Joined: 09 Apr 2015 13:18

Re: Creating a custom html generator in batch

#6 Post by hacxx » 20 Oct 2020 14:02

aGerman wrote:
20 Oct 2020 13:32
In what context did you face an issue?

Code: Select all

>"test.txt" echo "
Steffen
Sorry no issue, i didn't tested the output result.
I tried the command on cmd.exe and it's working fine, the only thing is i have to >file.txt before echoing.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating a custom html generator in batch

#7 Post by aGerman » 20 Oct 2020 14:09

Yes that's just best practice. It prevents you from a couple of issues besides of yours.

Code: Select all

echo 1 >"some.txt"
... has the issue that the space in front of > is redirected too.

Code: Select all

echo 1>"some.txt"
... has the issue that the 1 is treated as stream number and you'll get an "echo is off" redirected.

Code: Select all

>"some.txt" echo 1
... will work.

Steffen

Lucky4Me
Posts: 24
Joined: 21 Oct 2020 06:33

Re: Creating a custom html generator in batch

#8 Post by Lucky4Me » 21 Oct 2020 06:37

Mayby you can use the following

echo ^1>"some.txt"

it works, even for the next one

echo ^">"some.txt"

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating a custom html generator in batch

#9 Post by aGerman » 21 Oct 2020 11:14

Certainly you can replace critical characters with the escaped character. But the aim is to redirect user input into a file as I understood the original post.
So, you could use
copy con "foo.txt"
to achieve this, and finally press Ctrl+Z to create the file. Or read line by line using SET /P where you eventually want to avoid a long list of replacements. Proof of concept:

Code: Select all

@echo off &setlocal EnableDelayedExpansion
>nul chcp 65001

echo enter q to quit
>"foo.txt" call :input

echo(
echo(let's see what we got:
echo(
type "foo.txt"
pause
goto :eof

:input
set "line="
1>&2 set /p "line=new line: "
if "!line!"=="q" exit /b
echo(!line!
goto input
Steffen

CJM
Posts: 24
Joined: 25 Oct 2019 20:34
Location: Baltimore, MD USA

Re: Creating a custom html generator in batch

#10 Post by CJM » 25 Oct 2020 19:47

If you can do with the limit of not being able to use ? and * wildcard characters, I use a simple routine that leverages the command parser and FOR command.

Save this as EchoArgs.cmd:

Code: Select all

@	For %%* in (%*		%= Process each argument as a separate line. Arguments containing "poison"/whitespace must be quoted. =%
)do @	ECHO/%%~*		%= Remove any quotes. Fails, by omission or substitution of filenames, arguments that contain * or ? =%
Use like this to write/ECHO each argument, removing any outer quotes [""], as a separate line:

[Call] EchoArgs.cmd [arg1 | "arg 1" [ arg2 | "arg=2" [...]]
  • Supports many "poison" characters that normally would require escaping, including: & | < >
  • Quotes are required for "poison" & | < > ^ and white-space characters: , ; = [space] [tab]
  • Use command redirection as needed to create [>]/append [>>] to files
  • LIMITATION: Ignores/expands arguments containing wildcard characters: ? *
Examples:

Write HTML lines

Code: Select all

>File.HTML Call echoArgs "<!DOCTYPE><HTML><HEAD>" "<TITLE>HTML file</TITLE>" "</HEAD><BODY>"
Resulting File.HTML:

Code: Select all

<!DOCTYPE><HTML><HEAD>
<TITLE>HTML file</TITLE>
</HEAD><BODY>
Append HTML lines

Code: Select all

>>File.HTML Call echoArgs "<H1>Heading</H1>" "<P>Text</P>" "</BODY></HTML>"
Appends to File.HTML:

Code: Select all

<H1>Heading</H1>
<P>Text</P>
</BODY></HTML>
Note: Above comments from others regarding code page may need to be adapted to produce technically valid HTML files.

Write .CSV lines [note outer quotes]

Code: Select all

Call echoArgs ""Item","Description","Price"" ""ABC","A b c","$1.00"" ""XYZ","X yz","$9.00"" >>File.CSV
Resulting File.CSV:

Code: Select all

"Item","Description","Price"
"ABC","A b c","$1.00"
"XYZ","X yz","$9.00"

Post Reply