How do i center a InputBox In Batch?

Viewed 177
4 Answers

The set /P command trims leading white-spaces. But there is a nice work-around.

At first, do:

for /F %%B in ('prompt $H^& for %%Z in ^(.^) do rem/') do set "_BS=%%B"

to gather the back-space character. Then do:

set /P INPUT="#%_BS%    Prompt: "

to get leading SPACEs on the console display.

The trick is to begin the prompt text with a character other than a SPACE (like #), but then move the cursor back by a back-space (), so the first of the following SPACEs overwrites that character, thus deleting it.

Hence the actually printed character sequence is:

# SPACE SPACE SPACE SPACE Prompt: SPACE

which leads to the displayed text:

SPACE SPACE SPACE SPACE Prompt: SPACE

It is not centered in the middle because for it to be in the center, you have to write a code which will keep resetting the code every 1 ms and check the location where the center is then center it there.

This is the closest thing I found that might help you

echo                  URL:
set /p url=

but the input will be at the below and not centered.

ANSII color codes will help you.

for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set ESC=%%b
)

set /p url=%ESC%[30m------------------%ESC%[0mURL:
Related