使用.bat文件定制T恤命令 [英] Using a custom Tee command for .bat file

查看:181
本文介绍了使用.bat文件定制T恤命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use the tee code written for a bat file but am having trouble implementing it in my code. I don't want to use any third party installs to solve the tee problem as I want the code to work if I format my computer in a year and want to run the program again.

我把它以这种方式设置:

I have it setup in this fashion:

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

我试过用一个单独的的.bat 文件,并试图包括作为功能(preffered)原蝙蝠无济于事有:

I've tried with a seperate .bat file and tried including as a function (preffered) in the original .bat to no avail with:

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

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

I am trying to split the output so I can save the console output to a file while still being able to interact with the program that is running.

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

推荐答案

您尝试调用批处理功能在管道内,因为Windows管道如何工作总是会失败 - 通过新的CMD外壳的Windows实例化管道的两侧。请参见 http://stackoverflow.com/a/8194279/1012053 了解更多信息。

Your attempt to call a batch function within a pipe will always fail because of how Windows pipes work - Windows instantiates both sides of the pipe via new CMD shells. See http://stackoverflow.com/a/8194279/1012053 for more info.

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

That Rob van der Woude version of a batch tee cannot possibly work for you because it uses a FOR /F to read the results of a command - the command must execute to completion before any lines are read. That won't work if you need user interaction during the execution of the command. With that version of tee you might as well simply redirect output to a file and then TYPE the file when finished. Obviously not what you want.

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

There are pure batch tricks that can get you closer, but I think there is still one problem that can't be solved with pure batch. Your executable may put a prompt on a line without issuing a new line. I believe pure native batch always reads entire lines (except when at end of stream). I'm not aware of a batch method to read character by character.

小幅回调 - SET / P 可以读取管道输入的部分线路,但它有被用于一个强大的发球批次的解决方案,prevent它的局限性:有没有办法确切知道何时每一行结束。它仅限于1021字符,每个线。它从每一行的末端剥去控制字符。没有办法知道当它已到达输入流的结束。

Slight correction - SET /P can read partial lines of piped input, but it has limitations that prevent it from being used for a robust batch tee solution: There is no way to know for sure when each line ends. It is limited to 1021 characters per "line". It strips control characters from the end of each "line". There is no way to tell when it has reached the end of the input stream.

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

But there is a simple solution - JScript or VBScript works like a champ, and doesn't require any special installs. Here is a hybrid JScript/batch script that should work for you. The JScript is poorly written with lots of room for improvement. For example, there is no error checking.

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

I save the script as tee.bat. The first required argument specifies the name of the file to write to. By default, the file is over-written if it already exists. If a second argument is provided (value doesn't matter), then the output is appended to the file instead.

@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);
}

用法为pretty就像你所期望的。

Usage is pretty much like you would expect.

command.exe | tee.bat output.txt 1

最后1参数强制追加模式。它可以是1以外的任何值

The last 1 argument forces append mode. It could be any value besides 1

有可能把一切都在一个批处理脚本,你似乎preFER。

It is possible to put everything in one batch script as you seem to prefer.

@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);
}


更新


Update

有关在批处理脚本的学术兴趣的人,我在 Windows批处理贴球座的纯天然一批版本:'三通命令。但是,这种混合的方法是我的preferred脚本解决方案。

For anyone with an academic interest in batch scripting, I've posted a pure native batch version of tee at Windows batch: 'tee' command. But this hybrid approach is my preferred scripting solution.

这篇关于使用.bat文件定制T恤命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆