假设我有一个批处理文件,其中包含“ padding”,我想缩进提示字符串的开头,以供用户键入。如果我使用空格,则它将在运行时不显示,只是忽略了空格。该脚本为例:
@echo off
echo.
echo.
echo Hi.
echo Please input something.
echo.
set /P input=
在
=
之后有三个空格,我希望输入的标记远离命令框的边缘,但是这些空格将被忽略。我该如何解决这个问题?我正在使用Windows 7 SP1。
最佳答案
如上面的评论所述,Vista及更高版本在SET / P提示符下删除了前导空格。
解决该问题的方法是在提示中定义并使用退格字符。
::define a variable containing a single backspace character
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
set /p var=%BS% Leading spaces will not show properly.
通常提示会在一行的开头,因此上面的方法就可以了。
但是,如果提示是从一行的中间发出的(非常不寻常),则在
<BS>
之前应包含一个前导字符,因为退格键将删除之前的所有内容。<nul set/p=Leave the cursor at the end of this line:
set /p var=.%BS% The dot (any char) is necessary to prevent the <BS> from erasing the :
关于batch-file - 批量处理中,如何在输入提示字符串的开头创建空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9864620/