Home > Back-end >  Is there a way to have a multi line user input prompt in batch?
Is there a way to have a multi line user input prompt in batch?

Time:06-20

I've been trying to make a user input prompt that displays itself as multiple lines.

Example of how I want it to look:

Welcome to the program!
What would you like to do?
option [1]
option [2]
option [3]

This is how I tried to do that:

set /p selection="Welcome to the program! ^ What would you like to do? ^ option [1] ^ option [2] ^ option [3]"

But when I run that, it comes out like this:

Welcome to the program!  What would you like to do?  option [1]  option [2]  option [3]

I googled it but I couldn't find anything that helped, so if someone could tell me a way to do this that would be great!

CodePudding user response:

Maybe it's simple.

@echo off
echo welcome to program!
echo what would you like to do?
echo option[1]
echo option[2]
echo option[3]

set /p selection=

CodePudding user response:

There is a way to do it, and that is by creating a linefeed variable.

SET /P version:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Rem Define linefeed as a variable.
(Set BR=^
%NULL%
)

:AskIt
Set "VARIABLE="
Rem Delayed expansion is needed to use the linefeed variable.
SetLocal EnableDelayedExpansion
Set /P "VARIABLE=Welcome to the program^!!BR!What would you like to do?!BR!option [1]!BR!option [2]!BR!option [3]!BR!"
(Set VARIABLE) 2>NUL | %SystemRoot%\System32\findstr.exe /RX "^VARIABLE=[123]$" 1>NUL || GoTo AskIt
Echo You chose %VARIABLE%& Pause

EndLocal

CHOICE.EXE version (recommended)

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Rem Define linefeed as a variable.
(Set BR=^
%NULL%
)

Rem Delayed expansion is needed to use the linefeed variable.
SetLocal EnableDelayedExpansion
%SystemRoot%\System32\choice.exe /C 123 /N /M "Welcome to the program^!!BR!What would you like to do?!BR!option [1]!BR!option [2]!BR!option [3]!BR!"
Echo You chose %ERRORLEVEL%& Pause

EndLocal
  • Related