2

I've got a batch file written that runs a program we've developped, passing in a number of parameters. Whilst running the program in debug mode, a large number of debug messages are output, which it would be handy to review later, although I still want to be able to see the program running "live" as it were, within the cmd.exe window.

In a *nix environment I could use Tee to output to both the text file and stdout. Any suggestions?

2 Answers 2

6

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

some_program | TEE [ /D:nn ] file_name

Available as BAT, Perl and Regina scripts.

Author's note.

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.

Note: These scripts were written just for fun. Even though the do work, they cannot compete with the "real" stuff -- compiled executables, written in "true" programming languages by true programmers.


I use Cygwin.
There is also Tee utility for Windows updated

3
0

I have made a tee.bat tool for this:

@ECHO OFF
SETLOCAL
 SET Append=false 
IF /I "%~1]=="/a" ( SET Append=true & SHIFT )
IF "%~1"== "" GOTO help
IF "%~2"== "" GOTO help
 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 ) 
 SET File=%1
 DIR /AD %File% >NUL 2>NUL
 IF NOT ERRORLEVEL 1 ( SET File= & GOTO Syntax ) 
 SET Y= 
VER | FIND "Windows NT" > NUL 
IF ERRORLEVEL 1 SET Y=/Y
 IF %Append%==false  (COPY %Y% NUL %File% > NUL 2>&1) 
 FOR /F "tokens=1* delims=]" %%A IN ('FIND /N /V ""') DO (
    > CON ECHO.%%B  
        >> %File% ECHO.%%B
 ) 
ENDLOCAL 
GOTO:EOF
 :Count 
SET /A Counter += 1
 SET File=%1 
GOTO:EOF
 :help
 ECHO.
 ECHO Display text on screen and redirect it to a file simultaneously 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 Made by Wasif Hasan.

Usage: command | tee [/a] filename

/a switch append input to file rather than overwriting it.

1
  • doesn't work if command need arguments, i.e. command arg1 arg2 | tee filename
    – kstn
    Jan 30, 2023 at 16:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .