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:
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%....<NULPING %Computer% >NUL 2>&1IF 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.
.
@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.
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. :)
Comments (22)
Commented:
Commented:
Thank you t0t0 and all contributors.
Commented:
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)
Commented:
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
Commented:
Open in new window
and you can use echox everywhere in the current shell. Looks better than %echox%, IMO.View More