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

前端 未结 5 1954
一生所求
一生所求 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:47

    Highly inspired by in dbenham's answer, I propose a similar but simpler variation based in the fact that the backspace character can be inserted in its raw form (only in batch files, attempting this in the console directly won't work as expected):

    set /p var=.'BS' Leading spaces will now show properly.

    The 'BS' character can be inserted by typing Alt + Numpad 008 (8 is backspace's ASCII code, won't work using the alphanumeric keys typically found above the letters), using a good text editor (such as Notepad++, Windows Notepad simply performs the backspace action).

    If unable to insert the character, Notepad++ has a useful feature for this: in TextFX menu, choose TextFX Tools followed by Insert Ascii Chart or Character: Insert Ascii Chart or character The desired character is the BS (white letters on black background on the screenshot) found in line 9 (ASCII character 8 - as stated above - as it's a zero-indexed table).

    If still the result is not the one described, try changing the file encoding to ASCII. Using Notepad++:

    1. Make a backup copy of the script or perform an experiment in a separate file, as non-ASCII characters (accented character, non-Latin etc.) are lost in this conversion.
    2. In Encoding menu, choose Convert to ANSI
    3. Save and check the result again...

    Tested with Windows 7 Professional SP1.

    Credits also go to:

    • @DavidCallanan for questioning the obtained result.
    • Wadelau for writing about Notepad++ at ufqi.com (where the screenshot was extracted from).

提交回复
热议问题