19

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 when run, it just ignores the spaces. This script for an example:

@echo off

echo.
echo.
echo    Hi.
echo    Please input something.
echo.
set /P input=   

After the = there is three spaces, and what I expect is that the marker for input is away from the edge of the command box, however these spaces are ignored.

How can I fix this problem? I am using Windows 7 SP1.

4
  • Have you tried (set /p input= ) ?
    – Anders
    Mar 25, 2012 at 23:11
  • It worked for me in Windows XP SP3 (can't say for any other version at the moment). I mean, when the execution stopped for reading my input, the cursor was three spaces from the edge of the command window.
    – Andriy M
    Mar 25, 2012 at 23:11
  • Yes, I believe it might be broken on Vista+
    – Anders
    Mar 25, 2012 at 23:16
  • 2
    @Andiry M - I'm using Windows 7, this might be the reason why. It would still be nice to have a solution for this, though.
    – Markum
    Mar 25, 2012 at 23:17

7 Answers 7

35

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
3
  • Worked like a charm in Windows 10
    – Hunter S
    Oct 27, 2019 at 0:23
  • 1
    This doesn't answer the question, it's about adding spaces without line feed with set /p. How it can get more upvotes than the correct, accepted answer from dbenham?
    – jeb
    May 24, 2021 at 22:36
  • Because it works exactly the way the original question asked - without unnecessary program steps or gymnastics - and the set /P isn't necessary. Using "echo.", you can follow the "." with as many leading spaces as you wish. I have verified that this works in Win-10 Pro. Note that I have not verified this by exporting to a file, limiting myself to the scope of OP's question. Jan 9 at 16:01
10

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
6

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:

5
  • I tried this but after running the file I see this: .Ôùÿ Leading spaces will now show properly. Dec 10, 2016 at 15:36
  • @DavidCallanan: Note that the 'BS' is a single character Dec 11, 2016 at 21:19
  • I know, and I got the special backspace character to show up, however when I run the code the result is .Ôùÿ Leading spaces will now show instead of ` Leading spaces will now show` Dec 16, 2016 at 22:12
  • @DavidCallanan - please take a look at the "file encoding" tip which was recently added to the answer. If that doesn't work, please share your code page - get it by running cmd.exe and typing mode. Dec 18, 2016 at 1:43
  • Nit: even easier/more practical than the Insert Ascii Chart or Character feature, in Notepad++ one may simply get the ASCII Insertion Panel by going to Edit menu and choose Character Panel. Then simply select the desired entry (7/BEL in this case) and double-click to insert at the cursor position. Jan 3, 2017 at 11:39
1

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

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

You can use double quotes (" ... ") for your purpose

set /p OPTION="Your choice:   "

As you can see here, the cursor for input is 3 units away.

1
  • 1
    Did you read the title at the **beginning** of a input prompt string?
    – jeb
    Jun 23, 2022 at 12:22
0

I do it this way:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
for /F %%a in ('ECHO prompt $E ^| cmd') do (
    SET "ESC=%%a"
)
SET /p input=!ESC![0m   Give me some space  
ECHO Your input was "%input%"
ECHO.
ECHO To prevent trailing spaces beeing removed (e. g. by a formatter) you can also do something like:
SET /p input=!ESC![0m   Give me some space  !ESC![0m
ECHO This should be enough. But there is some more:
ECHO.
SET /p=!ESC![44m    Give me more space and color
SET /p="!ESC![0;95m        !ESC![4mGive me more space, color and spe(ia| (haracters and trailling space     "
SET /p="!ESC![0;104m          Give me more space, color and spe(ia| (haracters but please !ESC![0mstop the colors"

Terminal output

The last two cases requires double quotes due to some special characters. They could also be escaped with a circumflex (e. g. ^|) but if you have a lot of them it will become messy. The given solutions do not work (nicely) with quotation marks.

This solution relies heavily on this (awesome) answer: https://stackoverflow.com/a/38617204/16104038

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.