How do I move this batch choice description to a newline?
I have a batch script template that includes a choice in it, I want to have the question for what the end-user wants to do, and the choice description(s) below the question. Here is my template:
@ECHO OFF
Color __
TITLE %TITLE%
ECHO.
:choice
set /P c=Do you want to %CHOICE%? & ECHO.(%DESCRIPTION%)
if /I "%c%" EQU "Y" goto :choice_yes
if /I "%c%" EQU "C" goto :choice_cancel
goto :choice
:choice_cancel
echo %CHOICE% has been cancelled.
:choice_yes
:: Insert additional commands or code here.
pause
@ECHO OFF
As you can see in the script, I have already attempted & ECHO.(%DESCRIPTION%)
among other methods that insert a new line. I believe there needs to be an additional ECHO
before the actual choice question with the above method, but due to the structure of the code I don't know where to place it. What am I doing wrong? Is there perhaps a character that needs to be escaped due to syntax?
If you want the question and the input cursor to be above the description output, you'll need a way to move the cursor upwards. Batch scripts cannot do this alone (though PowerShell possibly could); you'd need a small external tool which uses the Windows Console APIs to move the cursor a few lines higher.
If you want only the question to be above the description, allowing the prompt to blink below everything, that's easy – just output the question using a separate command:
echo Do you want to %CHOICE%?
echo %DESCRIPTION%
set /p ANSWER=^>
echo You entered %ANSWER%.
Your original attempt doesn't work for a different reason.
The set
and the echo
are completely separate commands – it does not matter that you separate them with an &
instead of a line break; they are still executed individually:
- first the
set /p a=b
command is run, showing the prompt, then reading a single input line; - then, after input has been already read,
echo %description%
is run, showing your text.
So in other words, echo
does not insert a new line in the script; it tells the script to produce a new line in its output, but that's not the same thing at all.
(In other scripting/programming languages, to insert a new line in the script you would use \n
or just a literal line break, but Windows batch scripts again have no equivalent to that.)