0

I have a batch file that among other things sets up the environment and does a build. Both stdout (script output) and stderr (compiler errors) are redirected to separate files. Is it possible for a batch file to forcibly send a given command's output to the console, regardless of redirection on the batch file?

This could be for an important message that should not be redirected. Or more specifically, to put a color command in the script to set console foreground and background colors, to alert me to not reuse the console for other things (because the environment has been altered). When stdout is redirected, color will not change the console colors.

2

To demonstrate my comment:

:: Q:\Test\2018\08\10\SU_1347898.cmd
@Echo off
cls
( Echo normal output 1 redirected with the code block
  net file returns an error
  Echo *** this line should go directly to Console *** >CON:
  Echo normal output 2 redirected with the code block
) 1>SU_1347898_.txt 2>SU_1347898_Error.log
echo(
findstr "^" %~n0_*

Sample output:

*** this line should go directly to Console ***

SU_1347898_Error.log:Die Syntax dieses Befehls lautet:
SU_1347898_Error.log:
SU_1347898_Error.log:NET FILE
SU_1347898_Error.log:[id [/CLOSE]]
SU_1347898_Error.log:
SU_1347898_.txt:normal output 1 redirected with the code block
SU_1347898_.txt:normal output 2 redirected with the code block
2

As LotPings says in his comment and answer, you can always direct output to the console via >con (note, the trailing : is not needed).

But not all functionality works properly when directed to con. For example, cls does not properly clear the screen.

Another option is to save a copy of the original stdout before you redirect to a file, and then direct the special commands to the saved handle.

@echo off
9>&1 1>stdout.txt 2>stderr.txt (
  echo Most output goes to a file
  echo Hello world
  >&9 echo But this goes to the original definition of stdout - the console
)

Note that every time you redirect an existing handle, the original definition is saved to the first available undefined handle before the redirection is performed. So assuming there has not been any prior redirection, then &3 below will point to the original stdout (console), so there is no need for an explicit save to 9.

@echo off
1>stdout.txt 2>stderr.txt (
  echo Most output goes to a file
  echo Hello world
  >&3 echo But this normally goes to the original definition of stdout - the console
)

But I don't like to rely on the implicit save, because I can't guarantee 3 has not already been used, so I might not get the correct result. Best to explicitly save to an unused handle that you specify.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.