显示Windows命令提示符输出并将其重定向到文件

问题描述 投票:333回答:30

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

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

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

batch-file cmd stdout tee
30个回答
131
投票

要扩展davor's answer,您可以像这样使用PowerShell:

powershell "dir | tee test.txt"

如果您尝试在当前目录中重定向exe的输出,则需要在文件名上使用.\,例如:

powershell ".\something.exe | tee test.txt"

9
投票

我同意Brian Rasmussen的说法,unxutils端口是最简单的方法。在他的Batch FilesScripting Pages部分,Rob van der Woude提供了有关使用MS-DOS和CMD命令的大量信息。我认为他可能有一个原生的解决方案来解决你的问题,然后在那里挖掘后我找到了TEE.BAT,这似乎只是一个MS-DOS批处理语言实现的tee。这是一个非常复杂的批处理文件,我的倾向仍然是使用unxutils端口。


8
投票

如果您在Windows环境路径中有cygwin,则可以使用:

 dir > a.txt | tail -f a.txt

8
投票

我想对Saxon Druce的excellent answer进行一些扩展。

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

powershell ".\something.exe | tee test.txt"

但是,这只会将stdout记录到test.txt。它也没有记录stderr

显而易见的解决方案是使用这样的东西:

powershell ".\something.exe 2>&1 | tee test.txt"

但是,这对所有something.exes都不起作用。一些something.exes会将2>&1解释为一个论点而失败。正确的解决方案是只在something.exe周围有撇号,它的开关和参数,如下:

powershell ".\something.exe --switch1 --switch2 … arg1 arg2 …" 2>&1 | tee test.txt

6
投票

dir 1>a.txt 2>&1 | type a.txt

这将有助于重定向STDOUT和STDERR


4
投票

我也在寻找相同的解决方案,经过一番尝试,我成功地在命令提示符中实现了这一点。这是我的解决方案:

@Echo off
for /f "Delims=" %%a IN (xyz.bat) do (
%%a > _ && type _ && type _ >> log.txt
)
@Echo on

它甚至还捕获任何PAUSE命令。


3
投票

这是我根据其他答案之一使用的样本

@echo off
REM SOME CODE
set __ERROR_LOG=c:\errors.txt
REM set __IPADDRESS=x.x.x.x

REM Test a variable
if not defined __IPADDRESS (
     REM Call function with some data and terminate
     call :TEE %DATE%,%TIME%,IP ADDRESS IS NOT DEFINED
     goto :EOF
)

REM If test happens to be successful, TEE out a message and end script.
call :TEE Script Ended Successful
goto :EOF


REM THE TEE FUNCTION
:TEE
for /f "tokens=*" %%Z in ("%*") do (
     >  CON ECHO.%%Z
     >> "%__ERROR_LOG%" ECHO.%%Z
     goto :EOF
)

3
投票

我知道这是一个非常古老的话题,但在之前的答案中,并没有完全实现以批处理方式编写的实时Tee。我的解决方案是Batch-JScript混合脚本,它使用JScript部分来获取管道命令的输出,但数据处理在Batch部分完成。这种方法的优点是任何Batch程序员都可以修改该程序以满足特定需求。该程序还正确处理其他批处理文件生成的CLS命令的输出,即在检测到CLS命令输出时清除屏幕。

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

3
投票

这样的事情应该做你需要的吗?

%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

3
投票

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

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

2
投票
@echo on

set startDate=%date%
set startTime=%time%

set /a sth=%startTime:~0,2%
set /a stm=1%startTime:~3,2% - 100
set /a sts=1%startTime:~6,2% - 100


fullprocess.bat > C:\LOGS\%startDate%_%sth%.%stm%.%sts%.LOG | fullprocess.bat

这将创建一个包含当前日期时间的日志文件,您可以在此过程中使用控制台行


126
投票

我能够找到将输出重定向到文件然后重定向到控制台的解决方案/解决方法:

dir > a.txt | type a.txt

其中dir是需要重定向输出的命令,a.txt是存储输出的文件。


2
投票

这是MTS以前的answer的变体,但它添加了一些可能对其他人有用的功能。这是我使用的方法:

  • 命令被设置为变量,可以在以后的整个代码中使用,输出到命令窗口并附加到日志文件,使用set _Temp_Msg_Cmd= 该命令已使用胡萝卜redirection字符转义^,以便最初不评估命令
  • 创建一个临时文件,其文件名类似于正在运行的名为%~n0_temp.txt的批处理文件,该文件使用command line parameter extension syntax %~n0来获取批处理文件的名称。
  • 输出附加到单独的日志文件%~n0_log.txt

这是命令序列:

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

这是一个例子:

set _Temp_Msg_Cmd= ^> %~n0_temp.txt 2^>^&1 ^& type %~n0_temp.txt ^>^> %~n0_log.txt ^& type %~n0_temp.txt ^& del /Q /F %~n0_temp.txt

这样,命令可以简单地在批处理文件中的后续命令之后添加,该批处理文件看起来更清晰:

echo test message %_Temp_Msg_Cmd%

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

net use M: /D /Y %_Temp_Msg_Cmd%


1
投票

我使用带有“for”语句的批处理子例程来一次获取一行命令输出,并将该行写入文件并将其输出到控制台。

@echo off
set logfile=test.log

call :ExecuteAndTee dir C:\Program Files

Exit /B 0

:ExecuteAndTee
setlocal enabledelayedexpansion
echo Executing '%*'
  for /f "delims=" %%a in ('%* 2^>^&1') do (echo.%%a & echo.%%a>>%logfile%)
endlocal
Exit /B 0

1
投票

如果你在CLI上,为什么不使用FOR循环来“DO”你想要的任何东西:

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

Windows CMD for循环的重要资源:https://ss64.com/nt/for_cmd.html这里的关键是设置分隔符(delims),它会将每行输出分解为空。这样它就不会破坏默认的空白区域。 %a是一个任意字母,但它在“do”部分中使用,以及...对每行解析的字符执行某些操作。在这种情况下,我们可以使用&符号(&&)执行第二个echo命令来创建或追加(>>)到我们选择的文件。如果在编写文件时出现问题,请更安全地保留DO命令的顺序,我们至少首先将回声传递给控制台。第一个回显前面的at符号(@)禁止控制台显示echo-command本身,而只是显示命令的结果,该命令用于显示%a中的字符。否则你会看到:

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

更新:/ F跳过空白行,只修复是预先过滤输出,为每一行添加一个字符(可能通过命令查找行号)。在CLI中解决这个问题不是很快或很漂亮。此外,我没有包括STDERR,所以这里也捕获错误:

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

Redirecting Error Messages

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


1
投票

就像unix一样。

dir | tee a.txt

是否有效在Windows XP上,它需要安装mksnt

它会显示在提示上并附加到文件中。


0
投票

如果你想要在屏幕上看到的东西,即使批处理文件被重定向到文件,以下内容也会有所帮助。如果重定向到文件,也可以使用设备CON

例:

ECHO first line on normal stdout. maybe redirected
ECHO second line on normal stdout again. maybe redirected
ECHO third line is to ask the user. not redirected  >CON
ECHO fourth line on normal stdout again. maybe redirected

另请参阅良好的重定向说明:http://www.p-dd.com/chapter7-page14.html


0
投票

如何显示输出并将其重定向到文件。假设我使用dos命令dir> test.txt,此命令将输出重定向到文件test.txt而不显示结果。如何编写一个命令来显示输出并使用DOS将输出重定向到文件,即Windows命令提示符,而不是在UNIX / LINUX中。

您可能会发现biterscripting(http://www.biterscripting.com)中的这些命令很有用。

var str output
lf > $output
echo $output                            # Will show output on screen.
echo $output > "test.txt"               # Will write output to file test.txt.
system start "test.txt"                 # Will open file test.txt for viewing/editing.

0
投票

这实时工作,但也有点难看,性能很慢。还未经过充分测试:

@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

0
投票

另一种方法是在程序中将stdout发送到stderr:

在java中:

System.setOut(new PrintStream(new TeeOutputStream(System.out, System.err)));

然后,在你的dos批处理文件中:qazxsw poi

stdout将转到日志文件,stderr(相同的数据)将显示在控制台上。


0
投票

我在大多数机器上安装了perl,所以使用perl:tee.pl来回答

java program > log.txt

dir | perl tee.pl或dir | perl tee.pl dir.bat

原油和未经测试。


0
投票

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

my $file = shift || "tee.dat";
open $output, ">", $file or die "unable to open $file as output: $!";
while(<STDIN>)
{
    print $_;
    print $output $_;
}
close $output;

将以上内容保存为.bat文件。它还将文件流1上的文本输出拆分为文件流3,您可以根据需要重定向。在下面的示例中,我调用了上面的脚本splitPipe.bat ...

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

92
投票

有一个Unix tee命令的Win32端口,就是这样。见http://unxutils.sourceforge.net/http://getgnuwin32.sourceforge.net/


-5
投票

试试这个: dir> test.txt&type test.txt


48
投票

看看这个:wintee

不需要cygwin。

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

你也可以查看unxutils因为它包含tee(并且不需要cygwin),但要注意输出EOL在这里类似于UNIX。

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


43
投票

@ tori3852

我找到

dir > a.txt | type a.txt

没有工作(只有dir列表的前几行 - 怀疑某种过程分叉,第二部分,'type'命令在可怕列表完成之前终止?),所以我使用了:

dir > z.txt && type z.txt

它做了 - 顺序命令,一个在第二个开始之前完成。


22
投票

一个简单的C#控制台应用程序可以做到这一点:

using System;
using System.Collections.Generic;
using System.IO;

namespace CopyToFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            var buffer = new char[100];
            var outputs = new List<TextWriter>();

            foreach (var file in args)
                outputs.Add(new StreamWriter(file));

            outputs.Add(Console.Out);

            int bytesRead;
            do
            {
                bytesRead = Console.In.ReadBlock(buffer, 0, buffer.Length);
                outputs.ForEach(o => o.Write(buffer, 0, bytesRead));
            } while (bytesRead == buffer.Length);

            outputs.ForEach(o => o.Close());
        }
    }
}

要使用它,只需将source命令传递给程序,并提供要复制输出的任何文件的路径。例如:

dir | CopyToFiles files1.txt files2.txt 

将显示dir的结果,并将结果存储在files1.txt和files2.txt中。

请注意,上面的错误处理方式并不多(任何事情!),实际上可能并不需要支持多个文件。


21
投票

不幸的是没有这样的事情。

Windows控制台应用程序只有一个输出句柄。 (好吧,有两个STDOUTSTDERR,但这里没关系)>将通常写入控制台句柄的输出重定向到文件句柄。

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


18
投票

虽然它有点难看,但这有效:

dir >_ && type _ && type _ > a.txt

它比其他一些解决方案更灵活,因为它可以逐个语句地工作,因此您也可以使用它进行追加。我在批处理文件中使用这一点来记录和显示消息:

ECHO Print line to screen and log to file.  >_ && type _ && type _ >> logfile.txt

是的,您可以重复ECHO语句(一次用于屏幕,第二次重定向到日志文件),但这看起来一样糟糕,并且有点维护问题。至少这种方式您不必在两个地方对消息进行更改。

请注意,_只是一个简短的文件名,因此您需要确保在批处理文件的末尾删除它(如果您使用的是批处理文件)。


16
投票

mtee是一个小型实用程序,可以很好地实现此目的。它是免费的,源是开放的,它只是工作。

你可以在http://www.commandline.co.uk找到它。

在批处理文件中用于显示输出并同时创建日志文件,语法如下所示:

    someprocess | mtee /+ mylogfile.txt

其中/ +表示附加输出。

假设您已将mtee复制到PATH中的文件夹中,当然。

© www.soinside.com 2019 - 2024. All rights reserved.