Compartilhar via


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

Comments

  • Anonymous
    October 27, 2005
    Here are Win32 ports of some Unix commands:

    http://unxutils.sourceforge.net/

    sed has been a particular favorite of mine. :)
  • Anonymous
    October 28, 2005
    Personally, I'm a Cygwin fan...

    http://www.cygwin.com/
  • Anonymous
    July 17, 2006
    Can it work if the command outputs continously? .... like unix tee
  • Anonymous
    August 29, 2006
    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.
  • Anonymous
    August 29, 2006
    The comment has been removed
  • Anonymous
    March 06, 2007
    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:JunkTemp1>dir Volume in drive C has no label. Volume Serial Number is 202E-F715 Directory of C:JunkTemp1 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:JunkTemp1>

C:JunkScript> C:JunkScript>dir Volume in drive C has no label. Volume Serial Number is 202E-F715 Directory of C:JunkScript 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:JunkScript>xcopy . C:JunkTemp1 2>&1|mytee.bat C:Junkuseless2.txt