Even today, amidst promised integration and automation, when all else fails you still need to tap into the power of the terminal (command line/batch for Windows). I needed a way of piping output to both the console and a file from my batch file.
There are numerous references online that explain how to redirect console streams(e.g. Batch How To…Display & Redirect Output). There are actually three different types of streams:
- Standard Output
- Standard Error
In my case, I wanted the first two to be present in a file and in the console. The file allows for inspecting the output at a later time, while the console informs the user of their progress and any errors as they occur.
call process.bat 2>&1 | "%ProgramFiles(x86)%\GnuWin32\bin\tee.exe" -a output.log
There are several aspects of the above statement to notice:
- The 2>&1 part redirects the Standard Output and Standard Error while preserving the console.
- The latter part calls tee.exe, which receives the redirected output.
- The -a specifies output to a file. The next argument is that files name.
Tee is one of the many useful, compact and self-contained programs that are part of the GnuWin32 package. I have found them particularly useful when going beyond the basics of batch commands and utilities.