Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

batch file - How do I add a space on this line?

I'm making a domain resolver, and I don't like the way the text is at the window border. The line is set /p domainname=Enter Domain Name^> %ESC%[92m

I would like to add a space before where it says Enter Domain Name>

This is what it looks like

This is what it looks like

so it's not stuck right against the window border, which would make it look a bit nicer. How would I go about this? I haven't been able to find anything on how to solve this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As an alternative, you can use the prompt technique to display any text message (up to 511 characters long) without worrying about the leading white space characters.

It works as follows:

set "prompt=Your text message"
cmd /d /k <nul
set /p "myVar="

putting it in to a reusable function:

@echo off
setlocal EnableExtensions

call :getInput domainname " Enter Domain Name > %ESC%[92m"

echo domainname is [%domainname%]
pause
exit /b

:getInput <inputVar> ["Message String"]
setlocal DisableDelayedExpansion
set "Msg=%~2"
if defined Msg (
    REM For prompt strings, $ is an escape character so it should be escaped with another one.
    set "prompt=%Msg:$=$$%"
    "%COMSPEC%" /d /k <nul
)
endlocal
set /p "%~1="
exit /b

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...