Home > database >  How do I put a newline inside a "set /p" in batch
How do I put a newline inside a "set /p" in batch

Time:05-02

I'm trying to insert a new line inside the set /p variable prompt, here's an example.

set /p variableName=[1] - this\n[2] - or this

output:
[1] - this
[2] - or this

CodePudding user response:

I'm afraid it doesn't work the way you are thinking.

I found this, and created the following code where the variable PText contains the lines you want, then is used as the prompt in the SET /P command:

@ECHO OFF
SET PText=[1] - this^& ECHO;[2] - or this
SET /P variableName=%PText%
ECHO;Answer=[%variableName%]

When I run the code, it prints [1] - this, I answer ABC and press enter, it then prints [2] - or this, and the following ECHO command prints Answer=[ABC]:

[1] - thisABC
[2] - or this
Answer=[ABC]

I believe the only real answer would be to either ECHO what you want prior to doing the SET /P, or use the CHOICE command as described on this page.

Example SET /P:

ECHO;[1] - this
ECHO;[2] - or this
SET /P variableName=[1,2]?

CodePudding user response:

Like SomethingDark suggests, you could use simply echo for the lines

echo [1] This
set /p "var=[2] - or this "

But if you insist on only using set /p it can be done with

@echo off
setlocal EnableDelayedExpansion

(set \n=^
%=empty=%
)
set /p "var=[1] This!\n![2] or this"
  • Related