Home » Questions » Computers [ Ask a new question ]

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

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

I want to write some status info that replaces the last line when I do something.

Asked by: Guest | Views: 113
Total answers/comments: 4
Guest [Entry]

"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..."
Guest [Entry]

"@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."
Guest [Entry]

"You'd need a screen-cursor library like curses or ncurses, but I'm not overly familiar with their use. Both libraries were developed for Unix systems, but you can find them for Windows (in the Cygwin environment, or GnuWin32).

I don't know of any easy way to access them in a DOS-style batch file. You might be better off programming in a compiled language. Take a look at the Ncurses HOWTO to get a better idea of whether it will be useful."
"You'd need a screen-cursor library like curses or ncurses, but I'm not overly familiar with their use. Both libraries were developed for Unix systems, but you can find them for Windows (in the Cygwin environment, or GnuWin32).

I don't know of any easy way to access them in a DOS-style batch file. You might be better off programming in a compiled language. Take a look at the Ncurses HOWTO to get a better idea of whether it will be useful."
Guest [Entry]

"Solution 1

With Notepad++ it's possible to insert the Backspace ← character (ASCII 0x08) directly, using its ASCII Codes Insertion Panel (Edit > Character Panel).

My solution is to insert the [BS] character directly and then, like other solutions posted here, use it multiple times to delete previous characters:

Code

@ECHO OFF

SET ""BACKSPACE_x7=[BS][BS][BS][BS][BS][BS][BS]""

SET /P ""DUMMY_VAR=Step 1"" < NUL
REM Do some stuff...

SET /P ""DUMMY_VAR=%BACKSPACE_x7%Step 2"" < NUL
REM Do some other stuff...

GOTO :EOF

Notepad++ screenshot

Output

Solution 2

Another possibility is to use cecho (echo command with colors support) to insert a Carriage Return ↵ as a unicode character (U+000D):

Code

@ECHO OFF

SET CARRIAGE_RETURN={\u000D}

cecho Step 1
REM Do some stuff...

cecho %CARRIAGE_RETURN%Step 2
REM Do some other stuff...

GOTO :EOF

NB: cecho_x64.exe runs significantly faster on my machine than cecho.exe."