<

DOS: ECHO text to previous line - by Paul Tomasi

Published on
37,974 Points
26,874 Views
16 Endorsements
Last Modified:
Awarded
Community Pick
One of my most closely kept secrets is revealed in this discussion

How to output text on the same line


This question was recently posted in EE by Simon336697

Consider the problem:

ECHO Hello
ECHO World

Open in new window


This would output the following to the screen:

    Hello
    World

However, what if we wanted the words Hello and World to appear on the same line as in:

    Hello World

This could be done as follows:

ECHO Hello World

Open in new window


But this discussion is not about that. This discussion explores the possibility of outputting text to the previous line.


Now consider the following:

    output text to screen
    process other commands
    output more text to the same line

Impossible? Read on....


Firstly, let's use Simon336697's question as an example:

    1)  output "Searching...." to screen
    2)  PING
    3)  output (append) result to the same line

Before we continue, let's refine these instructions:

 1   )  output "Searching for %Computer%.... " to screen
 2   )  PING %Computer% >NUL 2>&1
 3.1)  if ping fails...
 3.2)     output "FAIL" to previous line
 3.3)  otherwise...
 3,4)     output "SUCCESS" to previous line
 3.5)  end-if

[step=""][Ed Note]: The 2>&1 portion simply redirects STDERR to STDOUT.  
Thus, the sequence >NULL 2>&1 totally silences the PING command.[/step]
And, this is what the code would look like in a DOS batch file:

ECHO Searching for %Computer%.... 

PING %Computer% >NUL 2>&1

IF ERRORLEVEL 1 (
   ECHO FAIL
) ELSE (
   ECHO SUCCESS
)

Open in new window


However, in the case of a successful PING, the output would still be as follows:

    Searching....
    SUCCESS

To enable ECHOing to the previous line then, the following syntax is used to suppress the cursor:

SET /P Var=text<NUL

Open in new window

where Var is any variablename and text is the text output to the screen.


Normally, SET /P Var=text takes it's input from STDIN, the standard input device. This is usually the keyboard.

Rather than SET /P= accepting input from STDIN, we can force it to accept input from another device, in this case, the NUL device. This is done by redirecting output from NUL to SET /P= however, because NUL does not produce any output, SET /P= waits until it receives input, in this case, from the ECHO command.

So, with only a minor change to our program, this would now look something like the following:

SET /P var=Searching for %Computer%....<NUL

PING %Computer% >NUL 2>&1

IF ERRORLEVEL 1 (
   ECHO FAIL
) ELSE (
   ECHO SUCCESS
)

Open in new window


And this time, in the case of a successful PING, the output is as follows:

    Searching for PC1....SUCCESS

where the variable %Computer% is set to PC1.

Finally, notice how PING's output is redirected to NUL. This is to ensure any output it produces is not sent to STDOUT, the standard output device, as SET /P= would capture this instead of capturing output from the ECHO command.
.
16
Author:t0t0
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free