Search code examples
batch-filecmdcommand-prompt

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.


Solution

  • 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%