heredoc for Windows batch?
Yes, very possible. ^ is the literal escape character, just put it before your newline. In this example, I put the additional newline in as well so that it is properly printed in the file:
@echo off
echo foo ^
this is ^
a multiline ^
echo > out.txt
Output:
E:\>type out.txt
foo
this is
a multiline
echo
E:\>
Not as far as I know.
The closest I know of is
> out.txt (
@echo.bla
@echo.bla
...
)
(@
prevents the command shell itself from printing the commands it's running, and echo.
allows you to start a line with a space.)
Here's another approach.
@echo off
:: ######################################################
:: ## Heredoc syntax: ##
:: ## call :heredoc uniqueIDX [>outfile] && goto label ##
:: ## contents ##
:: ## contents ##
:: ## contents ##
:: ## etc. ##
:: ## :label ##
:: ## ##
:: ## Notes: ##
:: ## Variables to be evaluated within the heredoc ##
:: ## should be called in the delayed expansion style ##
:: ## (!var! rather than %var%, for instance). ##
:: ## ##
:: ## Literal exclamation marks (!) and carats (^) ##
:: ## must be escaped with a carat (^). ##
:: ######################################################
:--------------------------------------------
: calling heredoc with results sent to stdout
:--------------------------------------------
call :heredoc stickman && goto next1
\o/
| This is the "stickman" heredoc, echoed to stdout.
/ \
:next1
:-----------------------------------------------------------------
: calling heredoc containing vars with results sent to a text file
:-----------------------------------------------------------------
set bodyText=Hello world!
set lipsum=Lorem ipsum dolor sit amet, consectetur adipiscing elit.
call :heredoc html >out.txt && goto next2
<html lang="en">
<body>
<h3>!bodyText!</h3>
<p>!lipsum!</p>
</body>
</html>
Thus endeth the heredoc. :)
:next2
echo;
echo Does the redirect to a file work? Press any key to type out.txt and find out.
echo;
pause>NUL
type out.txt
del out.txt
:: End of main script
goto :EOF
:: ########################################
:: ## Here's the heredoc processing code ##
:: ########################################
:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
set "line=%%A" && set "line=!line:*:=!"
if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
if "!line:~0,13!"=="call :heredoc" (
for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
if #%%i==#%1 (
for /f "tokens=2 delims=&" %%I in ("!line!") do (
for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
)
)
)
)
)
goto :EOF
Example output:
C:\Users\oithelp\Desktop>heredoc
\o/
| This is the "stickman" heredoc, echoed to stdout.
/ \
Does the redirect to a file work? Press any key to type out.txt and find out.
<html lang="en">
<body>
<h3>Hello world!</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</body>
</html>
Thus endeth the heredoc. :)
@echo off
for /f "delims=:" %%a in (
'findstr -n "^___" %0') do set "Line=%%a"
(for /f "skip=%Line% tokens=* eol=_" %%a in (
'type %0') do echo(%%a) > out.html
:: out.html
pause
goto: EOF
___DATA___
<!Doctype html>
<html>
<head>
title></title>
</head>
<body>
<svg width="900" height="600">
<text x="230"
y="150"
font-size="100"
fill="blue"
stroke="gray"
stroke-width="1">
Hello World
</text>
</svg>
</body>
</html>