对.bat文件使用定制的Tee命令

我正在尝试使用为bat文件编写的T恤代码,但是在我的代码中无法实现它。我不想使用任何第三方安装来解决上述问题,因为如果我一年内格式化计算机并希望再次运行该程序,我希望代码能够正常工作。


我以这种方式设置它:


mycommand.exe | tee.bat -a output.txt

我尝试过使用单独的.bat文件,并尝试在原始文件中包含(以功能形式).bat无济于事:


myprogram.exe | call tee -a output.txt

echo.

echo.

echo.

SET /P restart="Do you want to run again? (1=yes, 2=no): "

if "%restart%"=="1" GOTO LoopStart



::--------------------------------------------------------

::-- Function section starts below here

::--------------------------------------------------------

:tee

:: 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.11a for Windows NT 4 / 2000 / XP

ECHO Display text on screen and redirect it to a file simultaneously

ECHO.

IF NOT "%OS%"=="Windows_NT" ECHO Usage:  some_command  ¦  TEE.BAT  [ -a ]  filename

IF NOT "%OS%"=="Windows_NT" GOTO Skip

ECHO Usage:  some_command  ^|  TEE.BAT  [ -a ]  filename

:Skip

ECHO.

我正在尝试分割输出,以便可以将控制台输出保存到文件中,同时仍然能够与正在运行的程序进行交互。


我如何才能使Tee命令与.bat一起正常使用,以便可以将输出分为文件和控制台。


小怪兽爱吃肉
浏览 1005回答 3
3回答

湖上湖

不错,只有一部分I believe pure native batch always reads entire lines是错误的,我在一些多线程批处理中使用了它 

慕田峪9158850

这对我有用。您确定mycommand.exe输出到stdout而不是stderr吗?要捕获后者,您需要mycommand.exe 2>&1 | call tee ... 
打开App,查看更多内容
随时随地看视频慕课网APP