Vdmir40
Vdmir40

Reputation: 27

Is there any way to add multiple lines in a variable in a batch script?

What I'm talking is: I'm going to set up this variable with set varname = and the value is:

blablablabla
blablablabla

and I don't think this is possible(I tried)

set varname = 
    blablablabla
    blablablabla

and the bigger problem: It's an echo command, so I have to make the "blabla" thing display on the console. I tried this:

set varname = 
    echo blablablabla
    echo blablablabla

and of course, it didn't work. Any ideas please?

Upvotes: 1

Views: 158

Answers (2)

T3RR0R
T3RR0R

Reputation: 2951

To answer your follow up questions @Vdmir40, Yes you can use more lines than demostrated in Jebs example, and you can also use the Special Variable Jeb defined to print empty lines.

(set \n=^
%=empty, do not delete this line=%
)

Set line1=this is line one
Set line2=followed by line two
Set line3=ending in line three


setlocal EnableDelayedExpansion
set "myMultilineVar=%line1%!\n!%line2%!\n!!\n!%line3%"
echo !myMultilineVar!

pause >nul

All of what you asked Before and after Jebs answer is in Jeb's answer. Work with the code Jeb provided, change it around, understand how and why it works.

The above code adapted from Jebs answer will print as:

this is line one
followed by line two

ending in line three

Upvotes: 2

jeb
jeb

Reputation: 82337

if you want to embed a newline in the content of a variable you should predefine a special helper variable for this:

(set \n=^
%=empty, do not delete this line=%
)

This variable should always be used with delayed expansion.

setlocal EnableDelayedExpansion
set "myMultilineVar=line1!\n!line2"
echo !myMultilineVar!

Upvotes: 2

Related Questions