窗口 – 使用.bat文件的自定义Tee命令
我试图使用为bat文件编写的
tee代码,但在我的代码中无法实现它.我不想使用任何第三方安装来解决发球台问题,因为我希望代码在一年内格式化我的计算机,并希望再次运行该程序.
我以这种方式设置: mycommand.exe | tee.bat -a output.txt 我尝试使用一个单独的.bat文件,并尝试将原来的.bat中的函数(preffered)包含在内: 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正常工作,所以我可以将输出分成文件和控制台.
您尝试在管道中调用批处理函数将始终失败,因为Windows管道如何工作 – Windows通过新的CMD shell实例化管道的两侧.有关更多信息,请参阅
https://stackoverflow.com/a/8194279/1012053.
批量发球台的Rob van der 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之外,它可以是任何值 可以将所有内容放在您似乎喜欢的一个批处理脚本中. @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,true); var chr; while( !WScript.StdIn.AtEndOfStream ) { chr=WScript.StdIn.Read(1); WScript.StdOut.Write(chr); out.Write(chr); } 更新 对于对批处理脚本有学术兴趣的任何人,我已经在DosTips上发布了一个纯文本版本的第三版,在Asynchronous native batch tee script.但这种混合方式是我首选的脚本解决方案. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Windows本地管理员权限最佳做法
- .net – 无法从Windows Service连接到SQL LocalDB,WPF应用程
- windows-xp – 如何找出certutil的有效商店名称
- windows-phone-8 – 使用LongListSelector进行连续分页
- 如何在桌面应用程序中检测Windows 8.1
- windows – 更新批处理文件中的命令行参数
- windows-phone-7 – 使用MediaStreamSource的WP7 Shoutcast
- windows – 使用delphi确定进程虚拟大小
- 如何使用Windows命令行查找文件中字符串的出现次数?
- 在Windows Server 2008/2012上使用SCHTASKS需要帮助