In batch, how do I create spaces at the beginning of a input prompt string?

前端 未结 5 2031
一生所求
一生所求 2021-02-13 03:27

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

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 03:45

    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.

    But if the prompt is issued from the middle of a line (very unusual), then a leading character should be included prior to the , since the backspace will erase whatever was before it.

     from erasing the :
    

提交回复
热议问题