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

前端 未结 5 1960
一生所求
一生所求 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 <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 :
    
    0 讨论(0)
  • 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).
    0 讨论(0)
  • 2021-02-13 03:53

    You need to add a dot after the echo The following example will output "Test" with three leading spaces:

    echo.   Test
    

    Same works for tabulator. The following example will output "Test" with one leading tab:

    echo.   Test
    
    0 讨论(0)
  • 2021-02-13 04:04

    This works in every Windows OS from W2K + I've tried, if it suits you. You could just use a : in the string.

    set /p "var=Please input something: "
    echo.%var%
    
    0 讨论(0)
  • 2021-02-13 04:05

    dbenhams answer works good when you only want to display the text, but not if you create a file, as it inputs also the backspaces.

    But for files(and for displaying) you can use copy /a to remove a CR/LF with the help of a SUB(EOF) character.
    The trick is to append the SUB character directly after the text, so it's just before the CR/LF of the ECHO output.
    And then using the /a switch of the copy command will only copy the content to the SUB character, so the SUB and also the CR/LF are removed

    @echo off
    setlocal EnableDelayedExpansion
    call :createSub
    call :echoWithoutLinefeed "=hello"
    call :echoWithoutLinefeed " world"
    exit /b
    
    :echoWithoutLinefeed
    > txt.tmp (echo(%~1!sub!)
    copy txt.tmp /a txt2.tmp /b > nul
    type txt2.tmp
    del txt.tmp txt2.tmp
    exit /b
    
    :createSub
    copy nul sub.tmp /a > nul
    for /F %%a in (sub.tmp) DO (
       set "sub=%%a"
    )
    del sub.tmp
    exit /b
    
    0 讨论(0)
提交回复
热议问题