UNIX ports - TEE

TEE enables you to redirect standard output to a file and display it on screen simultaneously.

General syntax:

some_program | TEE [ /D:nn ] file_name
some_program
is the program whose output is to be filtered
file_name
is the file where some_program's output will be redirected to
nn
is some_program's maximum allowed idle time (in seconds) after which file_name will be closed (Rexx version only)

 

Examples:

DIR \\server\share\*.* | REGINA TEE.REX /D:3 C:\server.LOG

will display the directory of \\server\share on screen and redirect it to C:\server.LOG at the same time; if the DIR command stopped or pauses for 3 seconds, TEE will abort.
Display of the result will start immediately.

DIR \\server\share\*.* | PERL TEE.PL C:\server.LOG

will display the directory of \\server\share on screen and redirect it to C:\server.LOG at the same time.
Display of the result will start immediately.

DIR \\server\share\*.* | TEE.BAT C:\server.LOG

will display the directory of \\server\share on screen and redirect it to C:\server.LOG at the same time, after the DIR command has finished, and skipping empty lines.

 

The Rexx and Perl scripts will start displaying the result immediately.
For the batch file, what it boils down to is, you may just as well redirect the command's output to a file and then display that file afterwards. As a bonus, that way you won't skip empty lines.

 

Click to download the ZIPped sources
Download the ZIPped sources

 

TEE.BAT, Version 2.01 for Windows NT 4 / 2000 / XP

@ECHO OFF
:: Check Windows version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax

:: Keep variables local
SETLOCAL

:: Check command line arguments
SET Append=0
IF /I [%1]==[-a] (
	SET Append=1
	SHIFT
)
IF     [%1]==[] GOTO Syntax
IF NOT [%2]==[] GOTO Syntax

:: Test for invalid wildcards
SET Counter=0
FOR /F %%A IN ('DIR /A /B %1 2ˆ>NUL') DO CALL :Count "%%~fA"
IF %Counter% GTR 1 (
	SET Counter=
	GOTO Syntax
)

:: A valid filename seems to have been specified
SET File=%1

:: Check if a directory with the specified name exists
DIR /AD %File% >NUL 2>NUL
IF NOT ERRORLEVEL 1 (
	SET File=
	GOTO Syntax
)

:: Specify /Y switch for Windows 2000 / XP COPY command
SET Y=
VER | FIND "Windows NT" > NUL
IF ERRORLEVEL 1 SET Y=/Y

:: Flush existing file or create new one if -a wasn't specified
IF %Append%==0 (COPY %Y% NUL %File% > NUL 2>&1)

:: Actual TEE
FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
	>  CON    ECHO.%%B
	>> %File% ECHO.%%B
)

:: Done
ENDLOCAL
GOTO:EOF


:Count
SET /A Counter += 1
SET File=%1
GOTO:EOF


:Syntax
ECHO.
ECHO Tee.bat,  Version 2.10 for Windows NT 4 / 2000 / XP
ECHO Display text on screen and redirect it to a file simultaneously
ECHO.
ECHO Usage:  some_command  |  TEE.BAT  [ -a ]  filename
ECHO.
ECHO Where:  "some_command" is the command whose output should be redirected
ECHO         "filename"     is the file the output should be redirected to
ECHO         -a             appends the output of the command to the file,
ECHO                        rather than overwriting the file
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Modified by Kees Couprie
ECHO http://kees.couprie.org
ECHO and Andrew Cameron

Back to the top of this page...

 

TEE.PL, Version 2.00 for (ActiveState) Perl


#! perl

# Store help text in a variable
$syntax = "\nTee.pl,  Version 2.00\n";
$syntax = $syntax."Display text on screen and redirect it to a file simultaneously\n\n";
$syntax = $syntax."Usage:  some_command  \|  PERL.EXE  TEE.PL  filename\n\n";
$syntax = $syntax."Where:  \"some_command\" is the command whose output should be redirected\n";
$syntax = $syntax."        \"filename\"     is the file the output should be redirected to\n\n";
$syntax = $syntax."Written by Rob van der Woude\nhttp://www.robvanderwoude.com\n";

# Display help and quit if no arguments were specified
if ( !$ARGV[0] ) {
	print $syntax;
	exit(1);
}

# Open file for writing, or display help text on error
open( OUT, "> $ARGV[0]" ) || die( "\nCannot open file \"$ARGV[0]\"\n\n$syntax" );

# Read and redirect standard input
while ( <STDIN> ) {
	print $_;
	print OUT $_;
}

# Close output file
close OUT;

Back to the top of this page...

 

TEE.REX, Version 1.00 for (Regina) Rexx

/* TEE port */

/* Parse and check command line arguments */
Parse Upper Arg ."/D:"maxwait .
If maxwait = "" Then Do
	Parse Arg outfile dummy
	maxwait = 1
End
Else Do
	Parse Arg .":"maxwait outfile dummy
End
If outfile = "" Then Call Syntax
If dummy  <> "" Then Call Syntax
If maxwait = "" Then Call Syntax
If DataType( maxwait, "W" ) = 0 Then Call Syntax
If maxwait <  1 Then Call Syntax

/* Initialize variables */
stopflag = 0
counter  = 0

/* Initialize RexxUtil */
If RxFuncQuery( "SysLoadFuncs" ) <> 0 Then Do
	Call RxFuncAdd "SysLoadFuncs", "RexxUtil", "SysLoadFuncs"
End
Call SysLoadFuncs

/* Read, display and redirect standard input */
Do Until stopflag = 1
	If lines( STDIN ) = 0 Then Do
		counter = counter + 1
		If counter > maxwait Then stopflag = 1
		Call SysSleep 1
	End
	Else Do
		Parse Pull line
		Say line
		Call LineOut outfile, line
	End
End

/* Normal program termination */
Exit 0


Syntax:
	Say
	Say "Tee.rex,  Version 1.00 for (Regina) Rexx"
	Say "Port of Unix' TEE command"
	Say "Redirects its input to the console and to a file simultaneously"
	Say
	Say "Usage:  any_command  |  <REXX>  TEE.REX  [/D:nn]  output_file"
	Say
	Say 'Where:  "any_command" is the command whose output you want to redirect'
	Say '        "output_file" is the file where any_command'||"'s output is redirected to"
	Say '        "nn"          is the max idle time (in seconds) allowed for any_command'
	Say '        "<REXX>"      is your Rexx interpreter:'
	Say "                    - Windows:  REGINA.EXE with RexxUtil"
	Say "                    - OS/2:     no need to specify, just rename script to *.cmd"
	Say
	Say "Written by Rob van der Woude"
	Say "http://www.robvanderwoude.com"
	Say
	Exit 1
Return

Back to the top of this page...

 

And, last but not least, one of many "third party" versions available on the web is Joao Magalhaes' TEE for MS-DOS.