Page 1 of 1

How to replace value in same line in DOS window.

Posted: 16 Apr 2013 03:57
by level3
I have a batch file which automatically restart vlc.exe if the app crashes. I am also counting the number of times it crashes.

my current coding output is like:


VLC crashed for : 0 times.
VLC crashed for : 1 times.
VLC crashed for : 2 times.
VLC crashed for : 3 times.
VLC crashed for : 4 times.

However, I want my script to print the output like:

VLC crashed for : 0 times. then 1,2,3, and so on... I just want to replace the 0,1,2,3 but dont want to make new echo line.

Any idea? Thanks.

Re: How to replace value in same line in DOS window.

Posted: 16 Apr 2013 04:14
by foxidrive
A common way to display that is to use CLS before echoing.

The other way is to use backspaces, and is more convoluted.

Re: How to replace value in same line in DOS window.

Posted: 16 Apr 2013 04:21
by level3
Thnx, any code hint?

Now I'm doing "echo VLC Crashed for %count% times.

What to replace here?

Re: How to replace value in same line in DOS window.

Posted: 16 Apr 2013 04:34
by level3
Never mind, got it working with CLS.

Re: How to replace value in same line in DOS window.

Posted: 16 Apr 2013 07:07
by !k

Code: Select all

@echo off
setlocal enabledelayedexpansion

(echo.N cr.txt&echo.e 100 0d 0d&echo.r cx&echo.02&echo.w 100&echo.q)|debug>NUL
:: Assign a single CR to a variable, use a textfile with 2 bytes of 0x0d, 0x0d
for /f "tokens=1" %%x in ('type cr.txt') do set "CR=%%x"

::Simple sample, count from 1 to 2000, print it on the same line
for /L %%c in ( 1,1,20000) do (
   <nul set /p ".=Count: %%c!CR!"
)
echo.

Re: How to replace value in same line in DOS window.

Posted: 16 Apr 2013 07:19
by foxidrive
A method to get a CR without debug, as debug is not supported on 64 bit Windows.

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

Re: How to replace value in same line in DOS window.

Posted: 16 Apr 2013 08:17
by level3
wow... thanks !k this is exactly what i was looking for.

thank you all for sharing insights.