如何在Windows命令提示符中运行命令行应用程序,同时显示输出并将输出重定向到文件?

例如,如果我要运行命令dir > test.txt,这将把输出重定向到一个名为test.txt的文件,而不显示结果。

我如何写一个命令来显示输出并将输出重定向到Windows命令提示符中的文件,类似于Unix上的tee命令?


当前回答

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");

其他回答

这是MTS之前回答的一个变化,但是它增加了一些可能对其他人有用的功能。下面是我使用的方法:

A command is set as a variable, that can be used later throughout the code, to output to the command window and append to a log file, using set _Temp_Msg_Cmd= the command has escaped redirection using the carrot ^ character so that the commands are not evaluated initially A temporary file is created with a filename similar to the batch file being run called %~n0_temp.txt that uses command line parameter extension syntax %~n0 to get the name of the batch file. The output is appended to a separate log file %~n0_log.txt

下面是命令的顺序:

输出和错误消息被发送到临时文件^> %~n0_temp.txt 2^>^&1 临时文件的内容是: 附加到日志文件^&类型%~n0_temp.txt ^>^> %~n0_log.txt 输出到命令窗口^&键入%~n0_temp.txt 删除带有该消息的临时文件^& del /Q /F %~n0_temp.txt

下面是例子:

设置_Temp_Msg_Cmd = ^ > % ~ n0_temp.txt 2 ^ > ^ & 1 ^ & % ~型n0_temp.txt ^ > ^ > % ~ n0_log.txt ^ & % ~型n0_temp.txt ^ & del / Q / F % ~ n0_temp.txt

这样,命令就可以简单地附加在批处理文件中后面的命令之后,看起来更干净:

echo test message %_Temp_Msg_Cmd%

这也可以添加到其他命令的末尾。据我所知,当消息有多行时,它将工作。例如,如果有错误消息,以下命令将输出两行:

net使用M: /D /Y %_Temp_Msg_Cmd%

这是实时工作,但也是一种丑陋和性能缓慢。也没有经过很好的测试:

@echo off
cls
SET MYCOMMAND=dir /B
ECHO File called 'test.bat' > out.txt
for /f "usebackq delims=" %%I in (`%MYCOMMAND%`) do (
  ECHO %%I
  ECHO %%I >> out.txt
) 
pause

发送输出到控制台,附加到控制台日志,从当前命令删除输出

dir  >> usb-create.1 && type usb-create.1 >> usb-create.log | type usb-create.1 && del usb-create.1

不幸的是,没有这样的东西。

Windows控制台应用程序只有一个输出句柄。(嗯,有两个STDOUT, STDERR,但在这里不重要)>重定向输出通常写入控制台句柄到文件句柄。

如果你想要有某种多路复用,你必须使用一个外部应用程序,你可以将输出转移到它。然后,该应用程序可以再次写入文件和控制台。

看看这个:wintee

不需要cygwin。

不过,我确实遇到并报告了一些问题。

您还可以检查unxutils,因为它包含tee(不需要cygwin),但注意这里的输出EOL是类似unix的。

最后,但并非最不重要的是,如果你有PowerShell,你可以尝试Tee-Object。在PowerShell控制台中输入get-help tee-object获取更多信息。