Byron Whitlock
Byron Whitlock

Reputation: 53921

Do Batch files support multiline variables

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 "

Upvotes: 33

Views: 54558

Answers (10)

sdbbs
sdbbs

Reputation: 5492

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

Upvotes: 0

SwiftNinjaPro
SwiftNinjaPro

Reputation: 851

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

Upvotes: 3

Blackkatt
Blackkatt

Reputation: 13

I prefer this method

@ECHO OFF
SETLOCAL EnableDelayedExpansion

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

Upvotes: 0

user6366151
user6366151

Reputation:

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.

Upvotes: 3

user6366151
user6366151

Reputation:

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.

Upvotes: 0

Tom
Tom

Reputation: 2389

This is a pretty old question, but I put together a hybrid of the solutions from @Fabricio and @jeb that both worked correctly and added some readability:

setlocal enableDelayedExpansion
set NL=^


rem two empty line required
set var=this is a !NL! ^
multi !NL! ^
line !NL! ^
string !NL!

echo !var!

Upvotes: 3

Fabricio
Fabricio

Reputation: 7935

SET myFlags= ^
    a ^
    b ^
    c

Upvotes: 11

AlexL
AlexL

Reputation: 256

And now - without auxiliary SET commands:

echo this is a & echo multi & echo line & echo string

Upvotes: 1

jeb
jeb

Reputation: 82337

Or you can create a "real" newline character.

setlocal enableDelayedExpansion
set NL=^


rem two empty line required
echo first line !NL! second line
set multi=Line1!NL!Line2
set multi=!multi!!NL!Line3
echo !Multi!

With this variant the newline is a "normal" character in the string, so the variables act normally and you can assign them to another variable, this is not possible with the &echo. trick (which is useful for simple tasks).

Upvotes: 30

T1000
T1000

Reputation: 2941

Is that ok?

@echo off
set var=kur
set var2=kur2
echo var is = "%var%" and var2 is = %var2%

edit
is that what you mean ?

@echo off
set nl=^& echo.
echo this%nl%is%nl%multiline%nl%string

Upvotes: 17

Related Questions