-1

The formal way to redirect stderr to stdout in a cmd script is to use the 2>&1 option before running the script:
e.g. MyScript.cmd > output.txt 2>&1

Is there a way to redirect stderr to stdout in a middle of running?

(Pushing my code to a main function and calling to it with 2>&1 isn't solution for me due to unwanted side effects)

elady
  • 535
  • 6
  • 22
  • `dir > output.txt 2>&1` <--- this can be in the middle of a batch file. Better details would help us to understand what you are asking. – foxidrive Feb 27 '14 at 11:15
  • 1) if those side effects can be fixed, fix them. 2) your script won't write to stderr on its own, so technically what you want is to redirect other programs that you invoke; you can do that on their own command lines one by one. – Jon Feb 27 '14 at 11:25
  • Thanks @foxidrive, I mean if my script is `MyScript.cmd`, how can I redirect stderr of **this script** to stdout, not of a command in this script. – elady Feb 27 '14 at 11:25
  • As you have it written it will redirect STDERR to STDOUT during the running of `myscript.cmd`. All commands in the script using STDERR will go into STDOUT. – foxidrive Feb 27 '14 at 11:28
  • @foxidrive, but I want the redirection only after some first lines in this script. – elady Feb 27 '14 at 11:31
  • @Jon this means to scan a lot of lines, so I'm looking for an elegant solution :) Thank you. – elady Feb 27 '14 at 11:34
  • Without the script to see what is going on we are unable to provide a solution. But you can separate the batch file into two parts and redirect the second part after the initial portion is done. – foxidrive Feb 27 '14 at 11:49
  • How do you know from what point on you want to start the redirection? – Baruch Feb 27 '14 at 14:22
  • What language are you writing this in? Is it a batch file or some programming language? Which? – Baruch Feb 27 '14 at 14:23
  • possible duplicate of [Redirect stdout and stderr from inside a batch file](http://stackoverflow.com/questions/13399017/redirect-stdout-and-stderr-from-inside-a-batch-file) – Baruch Feb 27 '14 at 14:32

2 Answers2

1

Maybe you could use user defined handle to differentiate error streams.

MyScript.cmd

CommandICareAbout.exe 2>&3
CommandICareAbout2.exe 2>&3
CommandIDontCareAbout.exe

Usage

MyScript.cmd > output.txt 2>&1 3> errors.txt

You could also mute the commands you dont care about like this:

CommandIDontCareAbout.exe 2>nul
Vlastimil Ovčáčík
  • 2,799
  • 27
  • 29
0

As @baruch have linked in a comment, my problem was solved by this wrapping

2>&1 (
  command1
  command2
  .
  .
  EOF
)
elady
  • 535
  • 6
  • 22