How do I add a space on this line?

Viewed 251

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.

2 Answers

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

set /p strips all whitespace characters at the begin of the prompt string, so even if the prompt string were quoted then it still won't work. The trick is to write some non-whitespace character then overwrite it with CR

@echo off

setlocal EnableExtensions EnableDelayedExpansion

rem Get CR character
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

set /p "domainname=x!CR! Enter Domain Name > %ESC%[92m"
echo %domainname%
Related