批量处理中,如何在输入提示字符串的开头创建空格? [英] In batch, how do I create spaces at the beginning of a input prompt string?
问题描述
假设我有一个批处理文件,其中包含"padding",我想缩进提示字符串的开头,以供用户键入.如果使用空格,则在运行时它不会显示只是忽略空格.该脚本示例:
Let's say I have a batch file that has "padding" in it, and I want to indent the beginning of the prompt string for the user to type in. If I use spaces, it will not show up when run, it just ignores the spaces. This script for an example:
@echo off
echo.
echo.
echo Hi.
echo Please input something.
echo.
set /P input=
在=
之后有三个空格,我希望输入的标记远离命令框的边缘,但是这些空格将被忽略.
After the =
there is three spaces, and what I expect is that the marker for input is away from the edge of the command box, however these spaces are ignored.
如何解决此问题?我正在使用Windows 7 SP1.
How can I fix this problem? I am using Windows 7 SP1.
推荐答案
正如上面的评论所述,Vista和更高版本在SET/P提示符下删除了前导空格.
As the comments above state, Vista and beyond strip leading spaces in a SET /P prompt.
解决问题的方法是在提示中定义和使用退格字符.
The way to get around the problem is to define and use a backspace character in the prompt.
::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.
通常提示会在一行的开头,因此上面的方法就可以了.
Normally the prompt will be at the beginning of a line, so the above works just fine.
但是,如果提示是从一行的中间发出的(非常不寻常),则应该在<BS>
之前添加一个前导字符,因为退格键将删除之前的所有内容.
But if the prompt is issued from the middle of a line (very unusual), then a leading character should be included prior to the <BS>
, since the backspace will erase whatever was before it.
<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 :
这篇关于批量处理中,如何在输入提示字符串的开头创建空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!