Do Batch files support multiline variables

Viewed 44932

If so How?

Yes, batch files are lame, but I cannot use powershell, and I don't feel like writing a real app to do this simple task....

edit

What i want is somthing along the lines of

set var="this is a 
multi 
line 
string "
10 Answers

If you just wanted to use it for outputting the lines of variables, I hope this would be useful to you.


Inspired by something, I found this working.

For codes in normal Command Prompt:

set this=echo hi ^& echo bye

%this%

It gives out the two following two lines:

hi
bye

in the Command Prompt window.


The reason for using the ^ character:

Command Prompt would identify that you wanted to run two commands so that you need to add this character, that tells Command Prompt to recognize the next character as a symbol and not to do any special thing for it.

I think I figured it out. I had to add ^ to the %NL% var when putting it in the string, otherwise it hides the text after the first %NL% because its executing when being set, instead of when its being echoed later

set NL=^& echo

set str=text on line 1 ^%NL% text on line 2

echo %str%
text on line 1
text on line 2

If you wanted to build something instead, not just outputting, hope this would be useful to you too.


Firstly, you should make a file. Such as .txt. I'm going to use var.txt as an example.

Secondly, type in the multi-lines of variables.

Thirdly, here are the codes, but only works for batch:

@echo off
set num=0
set /p this%num%=<"var.txt"
goto there

:there
set /a num=num+1
for /f "skip=%num%" %%a in (var.txt) do (set this%num% =%%a
goto there)
set num=0
goto build

:build
rem Example of using multi-lines of variable.
call echo %%this%num% %%
set /a num=num+1
for /f "skip=%num% delims=" %%a in (var.txt) do (call echo %%this%num% %%
goto build)
rem Just replace the commands with `echo` part into something else which suits your situation.
pause >nul
exit

I admit that strictly speaking, this is not a variable of multi-line, but in my opinion, this is better than nothing.

You may ask why not just use the setlocal EnableDelayedExpansion command. The reason is simply, that command does work for me (IDK why). Therefore, I thought and made these codes that make the same effect.

I prefer this method

@ECHO OFF
SETLOCAL EnableDelayedExpansion

REM (define NewLine character)
(SET _NL=^
%=this line is empty=%
)

I just came to this kind of a specification; the NLM and NL are from https://stackoverflow.com/a/269819/6197439 - and then the trick is basically to write line by line, escaping the line ending with caret (^), and right before the caret, add a unique character (here +), which at the end will be replaced with actual newline via delayed expansion:

REM Creating a Newline variable (the two blank lines are required!)
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%
REM use a unique character (here +) to indicate a newline, then replace it later
SET banner=Line 1 +^
Line 2 +^
Line 3

setlocal enabledelayedexpansion
SET banner=%banner:+=!NL!%
setlocal disabledelayedexpansion
echo %banner%

This prints in terminal:

Line 1
Line 2
Line 3
Related