使用.bat文件自定义tee命令使用.bat文件自定义tee命令(Using a custom Te

2019-05-14 10:28发布

我试图用发球为bat文件编写的代码,但我有我的代码实现它的麻烦。 我不希望使用任何第三方安装,因为我想要的代码,如果格式化我的电脑在一年要再次运行程序工作,以解决三通问题。

我把它以这种方式设置:

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.
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

我试图把标准输出,所以我可以在控制台输出保存到文件中,同时仍然能够与正在运行的程序进行交互。

我怎样才能得到tee命令与我.BAT正常工作,这样我就可以把输出分解到两个文件和控制台。

Answer 1:

你试图调用一个管内的批处理功能总是会失败,因为Windows管道的工作方式 - 通过新的CMD壳的Windows实例化管道的两侧。 见https://stackoverflow.com/a/8194279/1012053获取更多信息。

这一批T恤的罗布范德Woude版本不可能为你工作,因为它使用FOR /F读取命令的结果-读取任何行之前的命令必须执行完毕。 如果你在命令执行过程中需要用户互动,将无法正常工作。 随着该版本三通你还不如干脆输出重定向到一个文件,然后TYPE完成后的文件。 显然不是你想要的。

有纯批技巧,可以让你更接近,但我认为仍然是无法用纯一批要解决的一个问题。 你的可执行文件可能把一个提示上线,而不会发出新的生产线。 相信纯天然批次总是读整行(除了在流的末尾时)。 我不知道一个批处理方式通过文字阅读的性格。

轻微修正 - SET /P可以读取传送输入的局部线,但它具有防止它被用于健壮批次三通溶液限制:没有办法肯定知道当每个线结束。 它被限制在1021个字每“行”。 它从每一“行”的端部条控制字符。 有没有办法知道,当它已经达到了输入流的末尾。

但有一个简单的解决方案 - JScript或VBScript的作品就像一个冠军,并且不需要任何特殊的安装。 这是一个混合的JScript /批处理脚本应该为你工作。 JScript的写得不好,有很多改进的余地。 例如,没有错误检查。

我脚本另存为tee.bat 。 第一个必需的参数指定写入文件的名称。 默认情况下,该文件是在写,如果它已经存在。 如果提供了第二个参数(值并不重要),然后输出被附加到文件来代替。

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

::--- Batch section within JScript comment that calls the internal JScript ----
@echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b

----- End of JScript comment, beginning of normal JScript  ------------------*/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var mode=2;
if (WScript.Arguments.Count()==2) {mode=8;}
var out = fso.OpenTextFile(WScript.Arguments(0),mode,true);
var chr;
while( !WScript.StdIn.AtEndOfStream ) {
  chr=WScript.StdIn.Read(1);
  WScript.StdOut.Write(chr);
  out.Write(chr);
}

用法是很像你所期望的。

command.exe | tee.bat output.txt 1

最后1个参数强制追加模式。 这可能是除了1的任意值

你似乎更喜欢它可以把一切都放在一个批处理脚本。

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

::--- Batch section within JScript comment ----------------------------
@echo off

::This block of code handles the TEE by calling the internal JScript code
if "%~1"=="_TEE_" (
  cscript //E:JScript //nologo "%~f0" %2 %3
  exit /b
)

::The rest of your batch script goes here

::This pipes to TEE in append mode
mycommand.exe | "%~f0" _TEE_ output.txt 1

exit /b

----- End of JScript comment, beginning of normal JScript  ------------------*/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var mode=2;
if (WScript.Arguments.Count()==2) {mode=8;}
var out = fso.OpenTextFile(WScript.Arguments(0),mode,true);
var chr;
while( !WScript.StdIn.AtEndOfStream ) {
  chr=WScript.StdIn.Read(1);
  WScript.StdOut.Write(chr);
  out.Write(chr);
}

更新

对于批处理脚本的学术兴趣的人,我已为三通一处纯天然的批量版本异步本地批处理脚本发球过在DosTips。 但是,这种混合的方法是我的首选脚本解决方案。



文章来源: Using a custom Tee command for .bat file