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

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

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


当前回答

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.

其他回答

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

@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

我想对撒克逊德鲁斯的精彩回答进行一点扩展。

如上所述,您可以重定向当前目录中的可执行文件的输出,如下所示:

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解释。

这样的东西能满足你的需要吗?

%DATE%_%TIME% > c:\a.txt & type c:\a.txt
ipconfig >> c:\a.txt & type c:\a.txt
ping localhost >> c:\a.txt & type c:\a.txt
pause

如果你在CLI中,为什么不用FOR循环来“DO”你想做的事情:

for /F "delims=" %a in ('dir') do @echo %a && echo %a >> output.txt

Great resource on Windows CMD for loops: https://ss64.com/nt/for_cmd.html The key here is setting the delimeters (delims), that would break up each line of output, to nothing. This way it won't break on the default of white-space. The %a is an arbitrary letter, but it is used in the "do" section to, well... do something with the characters that were parsed at each line. In this case we can use the ampersands (&&) to execute the 2nd echo command to create-or-append (>>) to a file of our choosing. Safer to keep this order of DO commands in case there's an issue writing the file, we'll at least get the echo to the console first. The at sign (@) in front of the first echo suppresses the console from showing the echo-command itself, and instead just displays the result of the command which is to display the characters in %a. Otherwise you'd see:

echo驱动器[x]中的卷为Windows

UPDATE: /F跳过空行,唯一的修复是预过滤输出,向每一行添加一个字符(可能通过命令find使用行号)。在CLI中解决这个问题既不快速也不漂亮。此外,我没有包括STDERR,所以这里捕获错误:

for /F "delims=" %a in ('dir 2^>^&1') do @echo %a & echo %a >> output.txt

重定向错误消息

插入符号(^)用于转义后面的符号,因为命令是一个正在被解释的字符串,而不是直接在命令行中输入它。

另一种变化是分割管道,然后根据需要重新定向输出。

    @echo off
    for /f "tokens=1,* delims=:" %%P in ('findstr /n "^"') do (
      echo(%%Q
      echo(%%Q>&3
    )
    @exit/b %errorlevel%

将上述内容保存到.bat文件中。它还将文件流1上的文本输出分割到文件流3,您可以根据需要重定向。在下面的例子中,我将上面的脚本称为splitPipe.bat…

    dir | splitPipe.bat  1>con  2>&1  3>my_stdout.log

    splitPipe.bat 2>nul < somefile.txt