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:

1:
2:
ECHO Hello
ECHO World


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:

1:
ECHO Hello World


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


And, this is what the code would look like in a DOS batch file:

1:
2:
3:
4:
5:
6:
7:
8:
9:
ECHO Searching for %Computer%.... 
 
PING %Computer% >NUL 2>&1
 
IF ERRORLEVEL 1 (
   ECHO FAIL
) ELSE (
   ECHO SUCCESS
)


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:

1:
SET /P Var=text<NUL

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:

1:
2:
3:
4:
5:
6:
7:
8:
9:
SET /P var=Searching for %Computer%....<NUL
 
PING %Computer% >NUL 2>&1
 
IF ERRORLEVEL 1 (
   ECHO FAIL
) ELSE (
   ECHO SUCCESS
)


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.
.