2

I'm Working on a Batch Game called BatchOS That basically imitates an Actual Operating System. While i was making the Internet Explorer for BatchOS. I Found something that i had to ask for. How do i Center a InputBox in Batch? Like there is no actual Questions about this. I wanted everything centered.

Screenshot:

The code says that The InputBox is centered but in Command Prompt. It's Not Centered at all.

I'm In Windows 11 Btw

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Falt
  • 23
  • 5

4 Answers4

4

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

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 2 Questions tho. How do you add those Key images things and how did you explain and understand your Own Code? – Falt Aug 21 '21 at 02:47
  • 1. Click the [edit] button beneath my answer, then you can see the plain text. 2. I think I can't quite follow, I only can tell I've got some experience with batch scripting… – aschipfl Aug 21 '21 at 09:58
  • Wow you have to Code in Stackoverflow to get Those cool Keyboard buttons? – Falt Aug 22 '21 at 04:42
  • This is called *Markdown* and is described [here](https://stackoverflow.com/help/formatting)… – aschipfl Aug 22 '21 at 12:39
1

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.

  • Do you know what code does that? Or you know a code doing that but you cant type in it – Falt Aug 20 '21 at 02:30
  • 1
    I think this question issue should help you. https://stackoverflow.com/questions/34729834/how-to-center-a-string-in-a-batch-file – Kush Mittal Aug 20 '21 at 02:37
  • I am trying to find a way to center the InputBox with this Forum but it didn't help. i can only center Text with it. Not InputBoxes :( – Falt Aug 20 '21 at 03:19
1

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.

Darwish
  • 49
  • 5
  • It is Below :(( – Falt Aug 20 '21 at 04:17
  • Wow this Question is really Hard to Solve – Falt Aug 20 '21 at 04:21
  • i just read somewhere that `set/p` will ignore if there's space/tab after it, and i think you can't use `set/p` for this. Maybe find another programming language – Darwish Aug 20 '21 at 05:11
  • [here](https://stackoverflow.com/questions/7105433/windows-batch-echo-without-new-line)'s where i read it, the first comment on the first answer – Darwish Aug 20 '21 at 05:14
  • well i would Switch to a new programming language. If I hadn't Already made as much progress as literally Releasing 2 Versions of My BatchOS Project – Falt Aug 20 '21 at 06:13
1

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:
rifteyy
  • 50
  • 6