How to split double quoted line into multiple lines in Windows batch file?

I see three possible workarounds.

1) Building the line combining multiple for-parameters.

@echo off
SETLOCAL EnableDelayedExpansion

set "line="
for %%a in ("line1" 
"line2"
"line3"
"line4"
) do set line=!line!%%~a
echo !line!

Drawback: It drops lines, when there is a ? or * in the text.

2) Leaving the “quote” at the end of each line

@echo on
SETLOCAL EnableDelayedExpansion

set "line=line1 & x#"^
 "line2 & a#"^
 "line3 & b #"^
 "line4 & c "

set "line=!line:#" "=!"
echo !line!

The first space in each line is important, because the caret works as multiline character but it also escapes the first character, so also a quote would be escaped.
So I replace the unnessary #” ” after building the line.

EDIT Added: 3) Disappearing quotes

setlocal EnableDelayedExpansion
echo "A very long line I want to !="!^
split into two lines"

In my opinion this is the best way, it works as the parser first see the quotes and therefore the last caret will work, as it seems to be outside of the quotes.
But this !="! expression will expand the variable named =", but such a variable name can’t exists (an equal sign can’t occur as first character) so it expands to nothing.

You can also create safe expressions, they will always escape out of quotes, independent if there is a quote or not in the line.
!="^"!

echo This multiline works !="^"!^
as expected
echo "This multiline works !="^"!^
too"

If you want avoid delayed expansion, you could also use a -FOR-Loop like

for %%^" in ("") do (
echo "This multiline works %%~"^
too"
)

Leave a Comment