30

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 "
Joey
  • 344,408
  • 85
  • 689
  • 683
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168

10 Answers10

28

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).

jeb
  • 78,592
  • 17
  • 171
  • 225
17

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
T1000
  • 2,909
  • 7
  • 36
  • 53
  • 1
    The only downside is that you can't redirect the output since it will redirect only the last line. To solve this you need to type `( ECHO this%nl%is%nl%multiline%nl%string )>NUL` – Ameen Dec 26 '14 at 07:02
  • 1
    @Ameen The reason is that `this%nl%is%nl%multiline%nl%string` is not a multiline string. It happens to expand to `this^& echo.is^& echo.multiline^& echo.string`. The outcome is the same as you'd got with only an `echo` and a multiline string but it's not just one `echo`: it's _four_ echos to get four lines. I would consider that cheating (or a party trick). – cdlvcdlv Jul 12 '18 at 17:52
8
SET myFlags= ^
    a ^
    b ^
    c
Fabricio
  • 7,705
  • 9
  • 52
  • 87
3

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
SwiftNinjaPro
  • 787
  • 8
  • 17
2

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!
Tom
  • 2,369
  • 13
  • 21
2

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.

  • Maybe my fault but I didn't found the above answers working, therefore I posted this... –  May 04 '18 at 15:31
  • It does works for batch-files for the "new line character" but I just wanted to add how to do the trick in normal Command Prompt environment. –  May 04 '18 at 17:02
1

And now - without auxiliary SET commands:

echo this is a & echo multi & echo line & echo string
AlexL
  • 246
  • 1
  • 2
  • 9
0

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.

jraufeisen
  • 3,005
  • 7
  • 27
  • 43
0

I prefer this method

@ECHO OFF
SETLOCAL EnableDelayedExpansion

REM (define NewLine character)
(SET _NL=^
%=this line is empty=%
)
Blackkatt
  • 13
  • 1
  • 5
0

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
sdbbs
  • 4,270
  • 5
  • 32
  • 87