I would like to write on the same line inside a loop in a windows batch file. For example:
setlocal EnableDelayedExpansion
set file_number=0
for %%f in (*) d
Thanks to the answer of MC ND I have a created a subroutine, echocr, that you can call without
delayed expansion, that will echo a string with only a carriage return,
and no newline. (The spaces after %input% are adjusted to cover all previous messages).
You can use it to overwrite a line as shown in the modified code:
@echo off
call :echocr "good morning"
PING -n 2 127.0.0.1>nul
call :echocr "good afternoon"
PING -n 2 127.0.0.1>nul
call :echocr "bye now"
PING -n 2 127.0.0.1>nul
pause
:echocr
:: (echo string with carriage return, no line feed)
for /F "tokens=1 delims=# " %%a in (
'"prompt #$H# & echo on & for %%b in (1) do rem"'
) do set "backspace=%%a"
set input=%~1
set "spaces40= "
set "spaces120=%spaces40%%spaces40%%spaces40%
for /f %%a in ('copy "%~f0" nul /z') do (
set /p ".=*%backspace%%spaces120%%%a"