2

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
miluuu
  • 29
  • 6

2 Answers2

6

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
phuclv
  • 37,963
  • 15
  • 156
  • 475
sst
  • 1,443
  • 1
  • 12
  • 15
2

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%
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • im confused as to where i would put this and what parts i would use it with(EDIT: i have gotten it to work but it messes up the ascii art that appears afterwords – miluuu Aug 13 '20 at 10:05
  • 1
    of course you put it in your batch file where you want to get user input. The `echo off` and `setlocal` and get CR should be set once at the beginning – phuclv Aug 13 '20 at 10:09
  • the setlocal command messes up my ascii art followed by a "ECHO is off" message in the middle of it – miluuu Aug 13 '20 at 10:12
  • 2
    @miluuu `!` gets stripped because of `delayedexpansion`, you should escape it with `^^!`, but this is totally out of your question's scope. – ScriptKidd Aug 13 '20 at 10:15
  • so, before every ! i should put ^^? – miluuu Aug 13 '20 at 10:16
  • 2
    yes, once DelayedExpansion is enabled `!` becomes a special character so you need to escape it if outside quotes – phuclv Aug 13 '20 at 10:26
  • ok ive done that and got it all fixed, thank you 2 for helping, but i got just one more question. Im using the ANSI Colors to help color specific things in my script, but it wont work when its next to %domainname%. im trying to put it like this `%ESC%[0m%domainname%` but it wont change the color, is it because it cannot work on something like %domainname%? My only thought is that i would need to add stuff so whenever %domainname% is written, it also applies that color code(%ESC%[0m) My only guess is i'd have to put this somewhere after something, `&& %ESC%[0m` – miluuu Aug 13 '20 at 10:40
  • 2
    @miluuu You can thank the user by selecting his answer correct one by selecting the grey tick mark next to the answer. As for the other part of the question about the colors. You can ask a new question and place your entire code in there and we can help. but [here is an answer](https://stackoverflow.com/questions/63050943/batch-script-issue-with-colored-echo-when-chaining-commands) I provided to something similar. – Gerhard Aug 13 '20 at 10:45