Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

DOS: ECHO text to previous line - by Paul Tomasi

Published:
Updated:
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
27,841 Views

Comments (22)

CERTIFIED EXPERT

Commented:
@paultomasi:Thanks for the feedback and example code.  An STDERR condition exists whenever the maximum directory/file length is encountered.  The purpose of the STDERR redirect is to create a clean .csv without the possibility of any embedded error messages.
Steven CarnahanAssistant Vice President\Network Manager
CERTIFIED EXPERT

Commented:
I just stumbled across this article.  There was a question just the other day that I was following.  I will certainly make use of this in some of my batch files.   :)

Thank you t0t0 and all contributors.
No probs...

BTW, since writing this article, I discovered you don't need an Lvalue so:

    set /p .=<nul

can also be written as:

    set /p =<nul

(I use a full-stop, or period, as an example but it could just as well be any allowable identifier)
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
I have used this method too in the past to make it more obvious (ish):

SET PRINT=^<NUL set /p =

Then in the area needed you can do:

%PRINT% What I want to print

or

SET echox=^<NUL set /p =
%echox% mytext

etc.

Steve
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Just to throw in another variant:
doskey echox=^<nul set /p=$*

Open in new window

and you can use echox everywhere in the current shell. Looks better than %echox%, IMO.

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community