Page 1 of 1
Creating a custom html generator in batch
Posted: 20 Oct 2020 04:31
by hacxx
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
Re: Creating a custom html generator in batch
Posted: 20 Oct 2020 06:17
by OJBakker
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
Re: Creating a custom html generator in batch
Posted: 20 Oct 2020 10:11
by aGerman
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
>.
Steffen
Re: Creating a custom html generator in batch
Posted: 20 Oct 2020 13:04
by hacxx
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
>.
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
Re: Creating a custom html generator in batch
Posted: 20 Oct 2020 13:32
by aGerman
In what context did you face an issue?
Steffen
Re: Creating a custom html generator in batch
Posted: 20 Oct 2020 14:02
by hacxx
aGerman wrote: ↑20 Oct 2020 13:32
In what context did you face an issue?
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.
Re: Creating a custom html generator in batch
Posted: 20 Oct 2020 14:09
by aGerman
Yes that's just best practice. It prevents you from a couple of issues besides of yours.
... has the issue that the space in front of > is redirected too.
... has the issue that the 1 is treated as stream number and you'll get an "echo is off" redirected.
... will work.
Steffen
Re: Creating a custom html generator in batch
Posted: 21 Oct 2020 06:37
by Lucky4Me
Mayby you can use the following
echo ^1>"some.txt"
it works, even for the next one
echo ^">"some.txt"
Re: Creating a custom html generator in batch
Posted: 21 Oct 2020 11:14
by aGerman
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
Re: Creating a custom html generator in batch
Posted: 25 Oct 2020 19:47
by CJM
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"