How to overwrite the same line in command output from batch file

Solution 1:

This script will do exactly what you asked:

@echo off

setlocal enableextensions enabledelayedexpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "ASCII_13=%%a"

set /p "=Step 1" <NUL
REM Do some stuff...

set /p "=!ASCII_13!Step 2" <NUL
REM do some other stuff...

I found this answer by looking at the code in a batch script character manipulation library written by Dave Benham called CharLib.

This library is able to create all characters in the range 1..255.

For more info see the thread entitled "Is it possible to convert a character to its ASCII value?"

Edit 2017-4-26: It appears that the code above no longer works. I'm unsure when this behaviour would have changed, but it definitely used to work. The CR character after the = now gets stripped by the set command. Ah, it appears to be a change in the way set works between XP and later versions of Windows. See the excellent answer in Overwrite line in Windows batch file? (duplicate) for more details and extra code examples.

An alternative then is to put a non-whitespace character before the !ASCII_13! and arrange for it to be erased too, perhaps either by also inserting a backspace (which doesn't get stripped) followed by a space or by appending spaces to the end of the prompt string (which aren't stripped.)

E.g. like this:

@echo off

setlocal enableextensions enabledelayedexpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "ASCII_13=%%a"

set /p "=Step 1" <NUL
REM Do some stuff...

set /p "=x!ASCII_13!Step 2 " <NUL
REM do some other stuff...

Solution 2:

To answer the question:

@echo off
CALL :EVALUATE "chr(8)"
SET backspace=%result%
<nul set /p =Step 1
:: DO SOME STUFF....
<nul set /p =%backspace%
<nul set /p =2
:: DO SOME OTHER STUFF....
<nul set /p =%backspace%%backspace%%backspace%%backspace%%backspace%%backspace%
<nul set /p =End   
GOTO:EOF



:EVALUATE           -- evaluate with VBS and return to result variable
::                  -- %~1: VBS string to evaluate
:: extra info: http://groups.google.com/group/alt.msdos.batch.nt/browse_thread/thread/9092aad97cd0f917

@IF [%1]==[] ECHO Input argument missing & GOTO :EOF 

@ECHO wsh.echo "result="^&eval("%~1") > %temp%\evaluate_tmp_67354.vbs 
@FOR /f "delims=" %%a IN ('cscript //nologo %temp%\evaluate_tmp_67354.vbs') do @SET "%%a" 
@DEL %temp%\evaluate_tmp_67354.vbs
::ECHO %result%
@GOTO:EOF

Output:

End

Basically, what the script does, is writes Step 1, then goes back one place, overwrites 1, and at the end goes completely back and overwrites Step 2 with End.

This going back I do with the special ASCII character 8 which is backspace. Because I don’t know how to write it in cmd, I use the :EVALUATE function which runs the VBS Chr() function, and puts the backspace-character into the variable result. If someone knows a better way, please advise.

Solution 3:

@echo off
for /F "tokens=1 delims=# " %%a in ('"prompt #$H# & echo on & for %%b in (1) do rem"') do set "BSPACE=%%a"
<nul set /p =Step 1
ping 127.0.0.1 >nul
<nul set /p =%BSPACE%
<nul set /p =2
ping 127.0.0.1 >nul
<nul set /p =%BSPACE%
<nul set /p =3
ping 127.0.0.1 >nul
<nul set /p =%BSPACE%%BSPACE%%BSPACE%%BSPACE%%BSPACE%%BSPACE%
<nul set /p =End.  
pause

EXPLANATION:

for /F "tokens=1 delims=# " %%a in ('"prompt #$H# & echo on & for %%b in (1) do rem"') do set "BSPACE=%%a"

this will set the backspace character into BSPACE variable. Now to see the result, type:

echo ab%BSPACE%c

output: ac

you can use this BSPACE variable more than once to delete multiple character.

Now, if you want to set a carriage return in a variable, use

for /F "usebackq" %%a in (copy /Z "%~dpf0" nul) DO (set "cr=%%a")

to see result, type: setlocal EnableDelayedExpansion & echo asfasdhlfashflkashflksa!CR!***

the output will be: ***asfasdhlfashflkashflksa

@davour's answer above is also beautiful but @timfoden's answer did not work for me.