OK, it took a while, but I finally understand your goal.
The following analysis assumes you are starting with default streams: 0 points to stdin, 1 points to stdout, 2 points to stderr, and all higher numbers are undefined. Also, stdin reads from the console, and stdout and stderr both write to the console.
When a pipe is in effect,
all stdout output is piped, regardless which number it came from.
EDIT - the solution has been simplified to Aacini's original solution - it works just fineIt sounds as though you want both 1 and 2 to go to the console, and 3 to go to the pipe. You have successfully defined 3 to go to the pipe, and you have redirected 1 to stderr so both 1 and 2 appear on the console without going to the pipe. But you are concerned that 1 and 2 have been merged such that they are synonymous. Presumably you want to be able to later redirect stdin or stdout independently, and are thinking that you can't. But you can
Code: Select all
@echo off
if "%1" equ "Restart" goto restart
"%~f0" Restart 3>&1 1>&2 | findstr /n "^"
exit /b
:restart
echo(
echo Normal
echo --------------
(
echo This must appear in the screen
rem The following error content
verify bad
echo Normal: This should go to stdin of findstr >&3
)
echo(
echo stderr to nul
echo --------------
2>nul (
echo This must appear in the screen
rem The following error should not appear
verify bad
echo stderr to nul: This should go to stdin of findstr >&3
)
echo(
echo stdout to nul
echo --------------
1>nul (
echo This should not appear on the screen
rem The following error should appear on the screen
verify bad
echo stdout to nul: This should go to stdin of findstr >&3
)
exit /b
But there is still a problem that is insurmountable - Normally stdin and stdout share the console and interleave properly based on time of output. But now the pipe also shares the console, and the pipe introduces a delay that can cause indeterminate results, thus leading to confusion.
The above code produces the following on my machine:
Code: Select all
Normal
--------------
This must appear in the screen
An incorrect parameter was
entered for the command.
stderr to nul
--------------
This must appear in the screen
1:Normal: This should go to stdin of findstr
stdout to nul
--------------
An incorrect parameter was
entered for the command.
2:stderr to nul: This should go to stdin of findstr
3:stdout to nul: This should go to stdin of findstr
All is good if the pipe output is redirected to a file, or if both stdin and stdout are redirected to files. For example:
Code: Select all
@echo off
if "%1" equ "Restart" goto restart
"%~f0" Restart 3>&1 1>&2 | findstr /n "^" >pipeOutput.txt
echo(
echo pipeOutput.txt
echo -------------
type pipeOutput.txt
exit /b
:restart
echo(
echo Normal
echo --------------
(
echo This must appear in the screen
rem The following error content
verify bad
echo Normal: This should go to stdin of findstr >&3
)
echo(
echo stderr to nul
echo --------------
2>nul (
echo This must appear in the screen
rem The following error should not appear
verify bad
echo stderr to nul: This should go to stdin of findstr >&3
)
echo(
echo stdout to nul
echo --------------
1>nul (
echo This should not appear on the screen
rem The following error should appear on the screen
verify bad
echo stdout to nul: This should go to stdin of findstr >&3
)
exit /b
--OUTPUT--
Code: Select all
Normal
--------------
This must appear in the screen
An incorrect parameter was
entered for the command.
stderr to nul
--------------
This must appear in the screen
stdout to nul
--------------
An incorrect parameter was
entered for the command.
pipeOutput.txt
-------------
1:Normal: This should go to stdin of findstr
2:stderr to nul: This should go to stdin of findstr
3:stdout to nul: This should go to stdin of findstr
Dave Benham