且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

批量处理中,如何在输入提示字符串的开头创建空格?

更新时间:2023-01-22 19:51:52

正如上面的评论所述,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 :