Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately, Windows 7 (and I think Vista as well) ignores leading white space in a <code>SET /P</code> prompt. Whitespace characters include space, tab, and non-breaking space (0xFF).</p> <p>The solution is not intuitive, but it is simple. Just prefix your prompt with a backspace (0x08) character. The code below programmatically defines a variable containing a backspace character. It is then easy to include it in any propmpt as needed.</p> <pre><code>@echo off setlocal ::Define a BS variable containing a backspace (0x08) character for /f %%A in ('"prompt $H &amp; echo on &amp; for %%B in (1) do rem"') do set "BS=%%A" echo x 1. Enter x echo x 2. Leave x echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxx echo: set /p Menu=%BS% Enter number {1,2}: </code></pre> <p>Normally a backspace will cause the line cursor to back up one position, and then the next character will overwrite what was there. But since the prompt always starts at the first position, there is no room to back up one. The backspace effectively does nothing accept allow for leading white space in your prompt :-)</p> <p>You can put another character in front of the backspace if you want, but it is not needed. The backspace will back the line cursor up one position, and then your desired prompt will overwrite the unwanted character.</p> <pre><code>set /p Menu=x%BS% Enter number {1,2}: </code></pre> <p><strong>EDIT - Here is an explanation of the <code>FOR /F</code> line that defines the BS variable</strong></p> <p>The <code>PROMPT</code> command controls the text that appears as a prefix of each line of output when ECHO is ON. By default it is the current directory followed by the <code>&gt;</code> character.</p> <p>Using <code>PROMPT $H</code> causes the prompt to be <code>&lt;backspace&gt;&lt;space&gt;&lt;backspace&gt;</code>. So a command like <code>REM</code> will result in output of <code>&lt;backspace&gt;&lt;space&gt;&lt;backspace&gt;REM</code>.</p> <p>There is a string of commands on one line that is executed. Normally commands are not echoed if they appear on the same line that issues the ECHO ON. The exception to that rule is any command that appears as part of a FOR DO clause <em>is</em> echoed. That is why the REM command is "executed" within a FOR loop - so we get the output of the REM command.</p> <p>The entire string of commands is executed within an outer <code>FOR /F</code> loop. The string is enclosed in single quotes to tell <code>FOR /F</code> to execute a command. Within the single quotes the string is enclosed within double quotes. The double quotes protect the command concatenation <code>&amp;</code> operator. Normally a string of commands cannot be enclosed in double quotes. But it works in this case because of how <code>FOR /F</code> works. The commands are executed implicitly via <code>CMD /C "command string"</code>. So it is perfectly acceptable in this case to use the double quotes.</p> <p>If double quotes are not used then the <code>&amp;</code> characters must be escaped with <code>^</code>:</p> <pre><code>('prompt $H ^&amp; echo on ^&amp; for %%B in (1) do rem') </code></pre> <p>Finally, since the default <code>FOR /F DELIMS</code> is <code>&lt;tab&gt;&lt;space&gt;</code>, the output of the REM command will be parsed into tokens, and only the first token is preserved. The end result is BS value consisting of a single <code>&lt;backspace&gt;</code> character.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload