echo output on same line

  • Thread starter Thread starter Dan Williams
  • Start date Start date
D

Dan Williams

Does anyone know if its possible to get the output of 2 different echo
commands to display on the same line?
ie. is there a 'echo -n' command as there is in Unix/Linux?

I'd like to do the following in a batch file:-

@echo off
echo.
echo Pinging Microsofts web site
ping www.microsoft.com| find /i "bytes=" > nul || goto NotConnected
echo ....OK
goto exit
:NotConnected
echo ....FAILED
:exit
echo.
pause

I'd like the OK or FAILED response to appear on the same line as the
'Pinging Microsofts web site' bit.

Thanks in advance

Dan Williams.
 
Dan Williams said:
Does anyone know if its possible to get the output of 2 different echo
commands to display on the same line?
ie. is there a 'echo -n' command as there is in Unix/Linux?

I'd like to do the following in a batch file:-

@echo off
echo.
echo Pinging Microsofts web site
ping www.microsoft.com| find /i "bytes=" > nul || goto NotConnected
echo ....OK
goto exit
:NotConnected
echo ....FAILED
:exit
echo.
pause

I'd like the OK or FAILED response to appear on the same line as the
'Pinging Microsofts web site' bit.

Hi Dan,

The only way I remember being able to do this is via ANSI control
characters. If you were processing this on a shell, used in a Telnet
session supporting say VT100 or ANSI emulation, you could do it I think -
otherwise you'd have to have anyone who used this batch file load ANSI.SYS
in their config/startup ("DEVICE=C:\WINNT\system32\ansi.sys").

The basic idea is that you use an escape-character sequence at the end of an
ECHO statement, (esc)[s actually, which saves the cursor position (at the
end of the line) in a register. When you have finished whatever processing
one needs, the next ECHO statement is preceded by an (esc)[u which restores
the cursor to that saved position - the end of your "Pinging Microsoft's web
site" sentence.

Now the only rub here is that unless you've got an editor that lets you put
escape codes into text, you'll either have to find one to do it, or use
something like DEBUG to add the sequence of characters in after you've
edited the batch file to have the proper number of spaces at the end and
beginning of the lines in question, something like:

ECHO Pinging Microsoft's Web Site (3 spaces added here)

and

ECHO (3 spaces added here) ...OK (or FAILED)

Using DEBUG to load the batch file, find the location of the first character
of the "Pinging" statement, and enter

e (address) "ECHO Pinging Microsoft's Web Site", 1B, "[s"

then for the other pair of statements, find the locations of their first
characters, and do the same:

e (address 1) "ECHO ", 1B, "[u...OK"
e (address 2) "ECHO ", 1B, "[u...FAILED"

Then save back to disk with 'W'. Mind you it's a lot easier with one of the
old tools o' the trade, the beloved BRIEF editor, and Alt-027 for the escape
code.

A fairly comprehensive list of the escape codes - strangely enough not in
the Knowledge Base! - may be found at
http://www.delmar.edu/Courses/CIS415L/ANSIsys.htm

Hope This Helps.
 
Cool, that worked a treat.

Many thanks

Dan.


Pegasus (MVP) said:
This should do the trick:

@echo off
echo.|set /p=Pinging Microsofts web site -
ping www.microsoft.com -n 1 | find /i "bytes="> nul
if ErrorLevel 1 (echo failed) else (echo OK)

Original idea by Phil Robyn.
 
Back
Top