Tee time

I've always felt that proficiency in the command language of an OS was an essential part of being able to write code for it.  I suppose it's possible to write code in a high-level language all day without knowing anything about writing batch files, but I wouldn't feel very good about my skills as a developer if I couldn't get around in my host OS's command language.  I think you have to know how to write a batch file or two just to have some street cred.  And I think being handy with batch files can save you some real work and make seemingly hard admin or build tasks much simpler, even with a batch file language as rickety and unwieldy as the one Windows offers.

Unix users like to brag about how superior their shell is to the Windows shell, and I have to admit there are some Unix commands I miss occasionally on Windows.  One of those is the tee command.  Tee allows you to direct the output from a command or batch file to the console and to a file simultaneously.  I wrote a batch file that simulated tee a few years ago, and I needed it again the other day, so I dug through my bag of tricks, found it, and blew the dust off of it.  Here it is for your enjoyment (save the script below as tee.cmd):

@echo off

IF (%1)==() GOTO help

::Overwrite the file (W2K/XP require /Y)
SET slash_y=
ver ¦ find "Windows NT" >nul
if ERRORLEVEL 1 set slash_y=/Y

::Overwrite the file
copy %slash_y% nul %1 >nul 2>&1

for /f "tokens=1* delims=]" %%A in ('find /V /N ""') do (
>con echo.%%B
>>%1 echo.%%B
)

GOTO :eof

:help
ECHO.
ECHO Pipe text to the console and redirect to a file simultaneously
ECHO.
ECHO Usage: command | tee filename