ravikanth
ravikanth

Reputation: 151

How to redirect STDIN, STDOUT, STDERR to a file in windows batch script

I have seen a number of examples where you can redirect the STD in/out/err of a single command to a file. So I was able to create a myLogger.bat as a wrapper that redirects the call statement of myScript to myLogFile.

Sample code of myLogger:

CALL "%xDir%\%%1" > "%lDir%\%lFil%" 2>&1

From the command line I execute> myLogger.bat myScriptToBeLogged.bat

However I was not able to redirect STD in/out/err of all the commands in a windows batch file(.bat)

Is there anything similar to "exec > $logFile 2>&1 in shell" that could be included in the beginning of the batch script which will redirect all the logs of the same script to a file.

Thanks in advance!

Upvotes: 2

Views: 4475

Answers (2)

dbenham
dbenham

Reputation: 130919

You almost have it. Simply CALL a routine at the beginning with redirection.

@echo off
>test.log 2>&1 call :start %*
exit /b

:start
REM rest of script goes here.

Upvotes: 1

David Bond
David Bond

Reputation: 308

Mostly, you will want to do this with 1> (STDOUT) and 2> (STDERR)

So, for example:

dir 1>stout.txt 2>stderr.txt

For more details, and how to do clever things with STDIN, see here.

Upvotes: 1

Related Questions