Overwrite line in Windows batch file? [duplicate]

Just before I say anything, I have tried this code:

@echo off

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

set /p "=Example 1" <NUL

set /p "=!ASCII_13!Example 2" <NUL

My Source: How to overwrite the same line in command output from batch file (The verified answer from it)

And that didn't work for me. It just outputted as Example 1Example 2 when I tried it.

So I just need a way to overwrite the same line in a batch file (eg. It said "Example 1" before, but after that line will say "Example 2" instead).

Thanks!


Solution 1:

<nul set /p=text is a trick to echo a string (the text prompt) without a line feed at the end, so the cursor will stay in the same line and we can later return to the start of the line (sending a carriage return) to overwrite it.

The problem you have found with the indicated answer is the behaviour change in the set /p. Up to Windows XP, the set /p command was able to echo spaces at the start of the text prompt, but from Vista white characters are removed from the start of the prompt. You can not send the carriage return at the start of the string. But you can use it in another place

@echo off
    setlocal enableextensions enabledelayedexpansion

    for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

    <nul set /p"=This is the line 1 !CR!"
    <nul set /p"=This is the line 2 "

That is, instead of sending the CR at the start of a line, send it at the end of the previous line.

Another option is to include a different character as the start of the text prompt before the CR. Including a visible character generates another problem, as it should be overwritten in the next line so it is not visible. A backspace (ascii 0x08) is a safe (usually) option

@echo off
    setlocal enableextensions enabledelayedexpansion

    for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
    for /f %%a in ('"prompt $H&for %%b in (0) do rem"') do set "BS=%%a"

    <nul set /p"=This is the line 1"
    <nul set /p"=!BS!!CR!This is the line 2"

Solution 2:

Since windows 10 Treshold2 the console understands
Ansi Escape Codes
These codes require the escape char hex 0x1b or decimal 27 which is
difficult to generate (depends on your editor)

The code to go to first column and n lines up is ESC[nF
The code to delete to the end of the line is ESC[0J

These two codes together (one line up):

    esc [ 1  F  esc [ 0  J  
hex 1B 5B 31 46 1B 5B 30 4A  

This Batch uses certutil to decode a hex string to ascii here

@echo off
echo 1B 5B>ESC.hex
Del ESC.bin >NUL 2>&1
certutil -decodehex ESC.hex ESC.bin >NUL 2>&1
Set /P ESC=<ESC.bin
set "Ansi=%ESC%1F%ESC%0J"

Echo line 1
Echo line 2
Echo line 3
Echo line 4

Timeout /t 3 >Nul

Echo %ESC%4F1st line
Timeout /t 1 >Nul
Echo 2nd line
Timeout /t 1 >Nul
Echo 3rd line
Timeout /t 1 >Nul
Echo 4th line

Echo New first Line extra long to see it's deleted, not overwritten
Timeout /t 3 >Nul
Echo %Ansi%Overwrite Line once
Timeout /t 2 >Nul
Echo %Ansi%Overwrite Line twice
Timeout /t 2 >Nul
Echo %Ansi%Overwrite Line thrice