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

In following example bat file, I want the text Step 2 to overwrite Step 1 on the same line in the command output.

echo off

echo Step 1
REM Do some stuff...

echo Step 2
REM do some other stuff...
link|improve this question

feedback

3 Answers

up vote 3 down vote accepted

You can't. Usually you can achieve this kind of thing by including a carriage-return character (0x0D) in the file which will put the cursor back to the first column in the same line. But in this case it doesn't work; the CR is just silently eaten.

Furthermore getting the CR in there is kinda tricky and at least here involved a text editor. I'd suggest you write a little utility program that will do this for you, it actually isn't very hard. The following little C program might suffice (if you don't need Unicode):

#include <stdio.h>

int main(int argc, char* argv[]) {
  if (argc < 2) return 1;
  printf("\r%s", argv[1]);
}

It does nothing more than printing a CR character and then the text you specify as its first argument. Usage as follows:

@echo off
<nul set /P X=Step 1
pause>nul 2>nul
over.exe "Step 2"

The last line is the call to that program. The second line is the normal batch idiom for printing text without a trailing line break (which is important in this case because otherwise you couldn't overwrite the line). You could just as well use the program above as well in that place, though.

A lightly hacky way, but the only one where you can be sure where you end up, would be to use cls prior to your step output. Will flicker but that way you always write to the upper-left. And clobber everything that was visible in the console (which is why I wouldn't recommend it); most users don't like that too much.

link|improve this answer
OK, I had a suspicion it was not possible to do with clean command line. If I want to make a utility do do this, I might as well put the entire thing in a utility. What I really wanted was just a visible countdown that wait 5 seconds, counting down each second till finished. – awe Dec 14 '09 at 7:59
2  
awe: If you are on Vista or later, then timeout 5 will work. – Joey Dec 14 '09 at 8:02
Great! thank you! timeout 5 works! – awe Dec 14 '09 at 8:30
2  
I should have posted my real issue the first time... – awe Dec 14 '09 at 8:31
feedback

You can't overwrite the contents of the bat file.

But an equivalent solution is to use a redirected echo command to create another bat file and execute it instead.

link|improve this answer
2  
He means, "on the screen". – gbarry Dec 14 '09 at 7:33
You are right gbarry. I meant on screen. I have changed the text to be more clear. – awe Dec 14 '09 at 7:47
Technically, a Windows cmd.exe batch file can write over itself while it's being run. – grawity Dec 14 '09 at 14:09
1  
@grawity: right, but who knows what will happen. – harrymc Dec 14 '09 at 14:33
feedback

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.

link|improve this answer
curses are pretty much for terminals and terminal emulators only. They don't really map to Windows's native console API. – Joey Dec 14 '09 at 9:17
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.