0

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
  • 6
    Wouldn't it make far more sense to simply `echo [1] - this` and then `set /p "variablename=[2] - or this"`? Or `echo` _both_ and then have no prompt at all? – SomethingDark May 02 '22 at 04:24

3 Answers3

0

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]?
Darin
  • 1,423
  • 1
  • 10
  • 12
  • No. The code you posted (which is non working and never will as it is a macro) is not the same code OP posted, it is therefore irrelevant. – Gerhard May 02 '22 at 14:41
  • PS. The code op posted is an example of what he requires and was not meant to be a working example. Just what was expected. – Gerhard May 02 '22 at 14:46
0

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"
jeb
  • 78,592
  • 17
  • 171
  • 225
  • @iNeedHelpWithThisCode, I appreciate you accepting my answer, but jeb's answer beats mine. I've never been on the asking end, so i don't know if you change his to the accepted answer, but you should if you can. He is doing what I had no clue could be done. His code actually works! I mostly understand his code, but I have to admit I'm shocked that it works. I think the `%=empty=%` is a non-existing variable, that prior to the parenthesis didn't exist, and expands to nothing. But why you need that , and how did anyone ever come up with that, is beyond me. – Darin May 12 '22 at 11:54
0

To actually insert a newline into the text:

@echo off
setlocal enabledelayedexpansion
set nl=^


::The Above empty lines are critical for this function, do not delete.
set /p "var=[1] - this!nl![2] - or this!nl!"
echo %var%

Personally I am not sure why you do not use choice for this?

echo [1] - This & echo [2] - or this & choice /c 12
Gerhard
  • 22,678
  • 7
  • 27
  • 43