Ken Henderson's WebLog

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

 

Published Thursday, October 27, 2005 11:03 PM by khen1234

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

robvolk said:

Here are Win32 ports of some Unix commands:

http://unxutils.sourceforge.net/

sed has been a particular favorite of mine. :)
October 28, 2005 7:23 AM
 

Adam Machanic said:

Personally, I'm a Cygwin fan...

http://www.cygwin.com/
October 28, 2005 12:40 PM
 

Anil said:

Can it work if the command outputs continously? .... like unix tee
July 18, 2006 5:40 AM
 

Paul said:

I've been looking for something like this, but your script doesn't provide real time output of stdout.  It's really not the same thing at all.
August 29, 2006 11:02 AM
 

khen1234 said:

Yes it does.  You're mistaken.  You need to redirect a process to it that does not buffer its output before sending it.  Most tools that use the MSxCRT buffer their output, and the developer is unaware of it because he doesn't have to do anything to enable it.  By default, the entirely of the console output of an app may be buffered if it doesn't send that much output in the first place.  This goes especially for batch file output.  Try this:  write a little test app that flushes stdout with each line of output it creates (but keeps stdout open the whole time).  Pipe it through this batch file.  You'll see that it does indeed provide real-time output and works very similarly to UNIX tee.
August 29, 2006 12:45 PM
 

Tomson said:

I saved the script as mytee.bat and tried to use it with xcopy command, the target directory and source directory contents are as follows. But it hung there without displaying anything.

C:\Junk\Temp1>dir

Volume in drive C has no label.

Volume Serial Number is 202E-F715

Directory of C:\Junk\Temp1

03/06/2007  01:33 PM    <DIR>          .

03/06/2007  01:33 PM    <DIR>          ..

03/05/2007  11:19 AM               396 a.bat

03/06/2007  01:14 PM                12 aaa.txt

03/05/2007  10:19 AM                20 b.bat

03/05/2007  08:20 AM                53 exclude.txt

03/05/2007  01:39 PM             2,502 mycopy.bat

03/05/2007  01:39 PM             2,502 mycopy.bat_for_email

03/05/2007  08:20 AM               885 mycopy.bat_original

03/05/2007  12:14 PM             2,256 mycopy1.bat

03/06/2007  12:02 PM             2,487 mycopyNoEmptyFolder.bat

03/06/2007  01:33 PM               455 mytee.bat

03/06/2007  01:12 PM                 0 useles.txt

03/06/2007  01:37 PM               921 useless.txt

03/06/2007  01:39 PM               788 useless1.txt

             13 File(s)         13,277 bytes

              2 Dir(s)  62,253,010,944 bytes free

C:\Junk\Temp1>

-----------------------------------------------

C:\Junk\Script>

C:\Junk\Script>dir

Volume in drive C has no label.

Volume Serial Number is 202E-F715

Directory of C:\Junk\Script

03/06/2007  01:33 PM    <DIR>          .

03/06/2007  01:33 PM    <DIR>          ..

03/05/2007  11:19 AM               396 a.bat

03/06/2007  01:14 PM                12 aaa.txt

03/05/2007  10:19 AM                20 b.bat

03/05/2007  08:20 AM                53 exclude.txt

03/05/2007  01:39 PM             2,502 mycopy.bat

03/05/2007  01:39 PM             2,502 mycopy.bat_for_email

03/05/2007  08:20 AM               885 mycopy.bat_original

03/05/2007  12:14 PM             2,256 mycopy1.bat

03/06/2007  12:02 PM             2,487 mycopyNoEmptyFolder.bat

03/06/2007  01:33 PM               455 mytee.bat

03/06/2007  01:12 PM                 0 useles.txt

03/06/2007  01:37 PM               921 useless.txt

             12 File(s)         12,489 bytes

              2 Dir(s)  62,253,043,712 bytes free

C:\Junk\Script>xcopy . C:\Junk\Temp1 2>&1|mytee.bat C:\Junk\useless2.txt

March 6, 2007 2:55 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker