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:

  1. Standard Output
  2. Standard Error
  3. Console

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.

Output redirection follows The Highlander Principle(there can be only one). Each of the output streams can go to a single destination. That is, before you introduce tee into the mix.

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s