- Community Pick
- Experts Exchange Approved
How to output text on the same line
This question was recently posted in EE by Simon336697
Consider the problem:
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:
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
- ""][Ed Note]: The 2>&1 portion simply redirects STDERR to STDOUT.
Thus, the sequence >NULL 2>&1 totally silences the PING command.
And, this is what the code would look like in a DOS batch file:
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:
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:
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.
.
by: Simon336697 on 2009-07-29 at 14:29:37ID: 2403
t0t0, that is so helpful thanks so much for this :>)