我正在制作一个域解析器,我不喜欢文本在窗口边框处的方式。这条线是
set /p domainname=Enter Domain Name^> %ESC%[92m
我想在
Enter Domain Name>
之前添加一个空格
这就是它的样子
所以它不会紧贴窗口边框,这会让它看起来更好一点。我该怎么办呢?我找不到任何有关如何解决此问题的信息。
作为替代方案,您可以使用 prompt 技术来显示任何文本消息(最多 511 个字符长),而不必担心前导空白字符。
其工作原理如下:
set "prompt=Your text message"
cmd /d /k <nul
set /p "myVar="
将其放入可重用函数中:
@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
会去除提示字符串开头的所有空白字符,因此即使提示字符串被引用,它仍然不起作用。诀窍是写入一些非空白字符,然后用 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%
即使在 Window 10 中也是可能的:
示例:您要写入“[”+带前导空格的字符串+“]
:: Leading marker
SET /P "=[" <NUL
:: Print string with leading blank and no newline
CALL :echon " Leading blank "
:: Ending marker
ECHO:]
应该产生:
[ Leading blank ]
这个片段应该可以解决问题:
:echon
SETLOCAL
:: Define _BS as backspace
@FOR /F %%a IN ('"PROMPT $H&FOR %%b IN (1) DO REM"') DO CALL SET "_BS=%%a"
:: Get string
SET "STR=%~1"
:: If first position is blank, replace with '${}'
IF " "=="%STR:~0,1%" SET STR=${}%STR:~1%
:: Print string - replace '${}' with '.'+backspace
SET /P "=!str:${}=.%_BS% !"<NUL
ENDLOCAL
GOTO:EOF :echon