The batch file test.bat below is functionally equivalent to your Unix script, that is, it sends all the standard output to a log file and displays an error on the screen:
@echo off if defined reEntry goto reEntry set reEntry=TRUE set AUTO_LOGFILE=%~N0.log rem stdout Redirection. Leave stderr as is. "%~F0" %* >>%AUTO_LOGFILE% :reEntry set reEntry= echo "Hello World" rem The above echo will be printed to test.log rem and error messages will be printed to the screen: verify badparam
Adenddeum:
I thought the OP wanted to separate stdout from stderr to see error messages on the screen, but maybe I misunderstood it. To send both stdout and stderr to a file, use the following line: dbenham noted in his comment:
"%~F0" %* >>%AUTO_LOGFILE% 2>&1
Or in an even simpler way, already expressed in the comment above:
@echo off set AUTO_LOGFILE=%~N0.log rem stdout and stderr Redirection. call :Main %* >>%AUTO_LOGFILE% 2>&1 goto :EOF :Main echo "Hello World" rem The above echo will be printed to test.log
However, if the goal is to delete error messages from the normal output, then this can be done on the same screen using this trick:
"%~F0" %* 2>&1 1>&3 | findstr /N /A:4E "^"
Thus, error messages precede a yellow line number on a red background. This method can be directly used in several cases; for example, in compiling any source program in a programming language:
anycompiler %1 2>&1 1>&3 | findstr /N /A:4E "^"
source share