如何在Windows命令提示符中运行命令行应用程序,同时显示输出并将输出重定向到文件?
例如,如果我要运行命令dir > test.txt,这将把输出重定向到一个名为test.txt的文件,而不显示结果。
我如何写一个命令来显示输出并将输出重定向到Windows命令提示符中的文件,类似于Unix上的tee命令?
如何在Windows命令提示符中运行命令行应用程序,同时显示输出并将输出重定向到文件?
例如,如果我要运行命令dir > test.txt,这将把输出重定向到一个名为test.txt的文件,而不显示结果。
我如何写一个命令来显示输出并将输出重定向到Windows命令提示符中的文件,类似于Unix上的tee命令?
当前回答
这是可行的,尽管它有点丑陋:
dir >_ && type _ && type _ > a.txt
它比其他一些解决方案更灵活一些,因为它是一条一条地工作的,所以您也可以使用它来追加。我在批处理文件中经常使用这种方法来记录和显示消息:
ECHO Print line to screen and log to file. >_ && type _ && type _ >> logfile.txt
是的,您可以只重复ECHO语句(一次用于屏幕,第二次重定向到日志文件),但这看起来同样糟糕,并且存在一些维护问题。至少这样您就不必在两个地方对消息进行更改。
注意_只是一个简短的文件名,所以你需要确保在批处理文件的末尾删除它(如果你使用的是批处理文件)。
其他回答
I agree with Brian Rasmussen, the unxutils port is the easiest way to do this. In the Batch Files section of his Scripting Pages Rob van der Woude provides a wealth of information on the use MS-DOS and CMD commands. I thought he might have a native solution to your problem and after digging around there I found TEE.BAT, which appears to be just that, an MS-DOS batch language implementation of tee. It is a pretty complex-looking batch file and my inclination would still be to use the unxutils port.
这不是另一个答案,更多的是对已经存在的答案的概述和澄清 显示Windows命令提示符输出并将其重定向到文件 和其他人
我自己发现,有一系列问题使得一组tee实现在Windows中不可靠(在我的情况下是Windows 7)。
我需要特别使用tee实现,因为已经使用了自重定向的批处理脚本:
@echo off
setlocal
... some conditions here ..
rem the redirection
"%COMSPEC%" /C call %0 %* 2>&1 | "<path_to_tee_utililty>" ".log\<log_file_name_with_date_and_time>.%~nx0.log"
exit /b
:IMPL
... here the rest of script ...
如果与tee实用程序一起使用,脚本和对脚本内一些实用程序的调用可能会中断输出。
gnuwin32的实现:
http://gnuwin32.sourceforge.net/packages/coreutils.htm
优点:
正确地处理标准输出和控制台进度条,其中\r字符被大量使用。
缺点:
使控制台进度条仅在日志文件中绘制,但在控制台窗口中没有复制或可见。 抛出多个错误消息Cwrite错误:没有这样的文件或目录,因为cmd解释器似乎过早地关闭了管道/stdout,并且在那之后不能自我关闭(垃圾邮件直到终止)。 不复制/打印控制台窗口中pause命令(按任意键继续…)的输出。
wintee实现:
https://code.google.com/archive/p/wintee/
https://github.com/rbuhl/wintee
优点:
在控制台窗口和日志文件(多次打印)中显示控制台进度条。 复制/打印控制台窗口中pause命令(按任意键继续…)的输出。
缺点:
错误地处理\r字符,输出是混合和混乱的(https://code.google.com/archive/p/wintee/issues/7)。 若有其他问题:https://code.google.com/archive/p/wintee/issues
UnxUtils实现:
http://unxutils.sourceforge.net/
https://sourceforge.net/projects/unxutils/files/unxutils/current/
Pros
在控制台窗口和日志文件(多次打印)中显示控制台进度条。 正确处理\r字符。 复制/打印控制台窗口中pause命令(按任意键继续…)的输出。
Cons
尚未发现
ss64.net实现:
http://ss64.net/westlake/nt
http://ss64.net/westlake/nt/tee.zip
优点:
在控制台窗口和日志文件(多次打印)中显示控制台进度条。
缺点:
错误地处理\r字符,输出混合和混乱 由于某种原因,在按下一个键后复制/打印控制台窗口中的pause命令(按任意键继续…)的输出。
ritchielawrence mtee实现:
https://ritchielawrence.github.io/mtee
https://github.com/ritchielawrence/mtee
Pros
在控制台窗口和日志文件(多次打印)中显示控制台进度条。 正确处理\r字符。 复制/打印控制台窗口中pause命令(按任意键继续…)的输出。 错误码保留特性w/o需要使用doskey的解决方案(/E标志,Windows命令解释器:如何获得第一个管道命令的退出码)
Cons
日志文件(https://github.com/ritchielawrence/mtee/issues/6)的路径不支持正斜杠字符 存在竞态条件问题,无法提取管道进程退出代码,因为它在访问之前已经关闭(https://github.com/ritchielawrence/mtee/issues/4)
因此,如果您在上述两种工具中选择tee实用程序实现,那么更好的选择是UnxUtils或mtee。
如果你正在寻找一个更好的实现,有更多的功能和更少的问题,那么你可以使用callf实用程序: https://github.com/andry81/contools/blob/trunk/Utilities/src/callf/help.tpl
你可以运行而不是:
call test.bat | mtee /E 1.log
这样的:
callf.exe /ret-child-exit /tee-stdout 1.log /tee-stdout-dup 1 "" "cmd.exe /c call test.bat"
它更好,因为它可以将stdout与stderr分开管道,甚至可以使用命名管道在具有管理员特权隔离的进程之间进行管道。
我想对撒克逊德鲁斯的精彩回答进行一点扩展。
如上所述,您可以重定向当前目录中的可执行文件的输出,如下所示:
powershell ".\something.exe | tee test.txt"
但是,这只将stdout记录到test.txt。它也不记录stderr。
最明显的解决方法是使用如下内容:
powershell ".\something.exe 2>&1 | tee test.txt"
然而,这并不适用于所有的前任。一些东西。前任们会把2>&1解释为争吵而失败。正确的解决方案是在something.exe及其开关和参数周围只使用撇号,如下所示:
powershell ".\something.exe --switch1 --switch2 … arg1 arg2 …" 2^>^&1 ^| tee test.txt
但是请注意,在这种情况下,您必须转义特殊的cmd-shell字符“>&|”,每个字符都有一个“^”,这样它们只能由powershell解释。
I know this is a very old topic, but in previous answers there is not a full implementation of a real time Tee written in Batch. My solution below is a Batch-JScript hybrid script that use the JScript section just to get the output from the piped command, but the processing of the data is done in the Batch section. This approach have the advantage that any Batch programmer may modify this program to fit specific needs. This program also correctly process the output of CLS command produced by other Batch files, that is, it clear the screen when CLS command output is detected.
@if (@CodeSection == @Batch) @then
@echo off
setlocal EnableDelayedExpansion
rem APATee.bat: Asynchronous (real time) Tee program, Batch-JScript hybrid version
rem Antonio Perez Ayala
rem The advantage of this program is that the data management is written in Batch code,
rem so any Batch programmer may modify it to fit their own needs.
rem As an example of this feature, CLS command is correctly managed
if "%~1" equ "" (
echo Duplicate the Stdout output of a command in the screen and a disk file
echo/
echo anyCommand ^| APATee teeFile.txt [/A]
echo/
echo If /A switch is given, anyCommand output is *appended* to teeFile.txt
goto :EOF
)
if "%2" equ ":TeeProcess" goto TeeProcess
rem Get the output of CLS command
for /F %%a in ('cls') do set "cls=%%a"
rem If /A switch is not provided, delete the file that receives Tee output
if /I "%~2" neq "/A" if exist %1 del %1
rem Create the semaphore-signal file and start the asynchronous Tee process
echo X > Flag.out
if exist Flag.in del Flag.in
Cscript //nologo //E:JScript "%~F0" | "%~F0" %1 :TeeProcess
del Flag.out
goto :EOF
:TeeProcess
rem Wait for "Data Available" signal
if not exist Flag.in goto TeeProcess
rem Read the line sent by JScript section
set line=
set /P line=
rem Set "Data Read" acknowledgement
ren Flag.in Flag.out
rem Check for the standard "End Of piped File" mark
if "!line!" equ ":_EOF_:" exit /B
rem Correctly manage CLS command
if "!line:~0,1!" equ "!cls!" (
cls
set "line=!line:~1!"
)
rem Duplicate the line in Stdout and the Tee output file
echo(!line!
echo(!line!>> %1
goto TeeProcess
@end
// JScript section
var fso = new ActiveXObject("Scripting.FileSystemObject");
// Process all lines of Stdin
while ( ! WScript.Stdin.AtEndOfStream ) {
// Read the next line from Stdin
var line = WScript.Stdin.ReadLine();
// Wait for "Data Read" acknowledgement
while ( ! fso.FileExists("Flag.out") ) {
WScript.Sleep(10);
}
// Send the line to Batch section
WScript.Stdout.WriteLine(line);
// Set "Data Available" signal
fso.MoveFile("Flag.out", "Flag.in");
}
// Wait for last "Data Read" acknowledgement
while ( ! fso.FileExists("Flag.out") ) {
WScript.Sleep(10);
}
// Send the standard "End Of piped File" mark
WScript.Stdout.WriteLine(":_EOF_:");
fso.MoveFile("Flag.out", "Flag.in");
看看这个:wintee
不需要cygwin。
不过,我确实遇到并报告了一些问题。
您还可以检查unxutils,因为它包含tee(不需要cygwin),但注意这里的输出EOL是类似unix的。
最后,但并非最不重要的是,如果你有PowerShell,你可以尝试Tee-Object。在PowerShell控制台中输入get-help tee-object获取更多信息。