How do I change a character in the console on Command Prompt?

batch-file, cmd

Solution

You can do this with batch. Windows cmd doesn't have direct access to the display, but you can use the carriage return or also the backspace character.

@echo off
setlocal EnableDelayedExpansion

REM ** Create a single carriage return character in the `CR` variable
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

REM ** Count from 3 to 0 down
REM ** ECHO for each number the line without linefeed
REM ** And wait a second with the ping command
for /L %%n in (3 -1 0) do (
  <nul set /p=Do something %%n!CR!
  ping localhost -n 2 > nul
)
echo(

The first part is only to set a carriage return character to the `CR` variable.

The `<nul set /p =Text` is a trick to display text without a linefeed, so the cursor stands after the last character. But in this case I appended the `!CR!`, so the cursor moves back to the first column in the same line.

Problem

I want my .bat script to do something like this in the console: ``` C:\Users\username>somescript.bat Doing something in: 3 ``` One second later... ``` C:\Users\username>somescript.bat Doing something in: 2 ``` How would I do something like that? Also, I do not want to clear the console, only change one thing on the window.

Original source