355 cmd stdout batch-file tee
如何在Windows命令提示符下运行命令行应用程序并同时显示输出并重定向到文件?
例如,如果我要运行该命令dir > test.txt,则会将输出重定向到调用的文件test.txt而不显示结果.
我怎么能写一个命令显示输出和输出重定向到在Windows命令提示符下一个文件,类似于teeUnix上的命令?
Sax*_*uce 140
要扩展davor的答案,您可以像这样使用PowerShell:
powershell "dir | tee test.txt"
Run Code Online (Sandbox Code Playgroud)
如果您尝试在当前目录中重定向exe的输出,则需要.\在文件名上使用,例如:
powershell ".\something.exe | tee test.txt"
Run Code Online (Sandbox Code Playgroud)
NSP*_*GNk 130
我能够找到将输出重定向到文件然后重定向到控制台的解决方案/解决方法:
dir > a.txt | type a.txt
Run Code Online (Sandbox Code Playgroud)
其中dir是需要重定向输出的命令,a.txt是存储输出的文件.
Bri*_*sen 92
有一个Unix tee命令的Win32端口,就是这样做的.请参阅http://unxutils.sourceforge.net/或http://getgnuwin32.sourceforge.net/
小智 43
@ tori3852
我找到
dir > a.txt | type a.txt
Run Code Online (Sandbox Code Playgroud)
没有工作(只有dir列表的前几行 - 怀疑某种过程分叉,第二部分,'type'命令在可怕列表完成之前终止?),所以我使用了:
dir > z.txt && type z.txt
Run Code Online (Sandbox Code Playgroud)
它做了 - 顺序命令,一个在第二个开始之前完成.
小智 23
一个简单的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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
要使用它,只需将source命令传递给程序,并提供要复制输出的任何文件的路径.例如:
dir | CopyToFiles files1.txt files2.txt
Run Code Online (Sandbox Code Playgroud)
将显示dir的结果,并将结果存储在files1.txt和files2.txt中.
请注意,上面的错误处理方式并不多(任何事情!),实际上可能并不需要支持多个文件.
Dan*_*ski 21
不幸的是没有这样的事情.
Windows控制台应用程序只有一个输出句柄.(好吧,有两个STDOUT,STDERR但这里没关系)>将通常写入控制台句柄的输出重定向到文件句柄.
如果你想要某种多路复用,你必须使用一个外部应用程序,你可以将输出转移到.然后,此应用程序可以再次写入文件和控制台.
MTS*_*MTS 19
虽然它有点难看,但这有效:
dir >_ && type _ && type _ > a.txt
Run Code Online (Sandbox Code Playgroud)
它比其他一些解决方案更灵活,因为它可以逐个语句地工作,因此您也可以使用它进行追加.我在批处理文件中使用这一点来记录和显示消息:
ECHO Print line to screen and log to file. >_ && type _ && type _ >> logfile.txt
Run Code Online (Sandbox Code Playgroud)
是的,您可以重复ECHO语句(一次用于屏幕,第二次重定向到日志文件),但这看起来一样糟糕,并且有点维护问题.至少这种方式您不必在两个地方对消息进行更改.
请注意,_只是一个简短的文件名,因此您需要确保在批处理文件的末尾删除它(如果您使用的是批处理文件).
小智 16
mtee是一个小型实用程序,可以很好地实现此目的.它是免费的,源是开放的,它只是工作.
您可以在http://www.commandline.co.uk找到它 .
在批处理文件中用于显示输出并同时创建日志文件,语法如下所示:
someprocess | mtee /+ mylogfile.txt
Run Code Online (Sandbox Code Playgroud)
其中/ +表示附加输出.
假设您已将mtee复制到PATH中的文件夹中,当然.
小智 11
我想对Saxon Druce的优秀答案进行一些扩展.
如上所述,您可以在当前目录中重定向可执行文件的输出,如下所示:
powershell ".\something.exe | tee test.txt"
Run Code Online (Sandbox Code Playgroud)
但是,这只会记录stdout到test.txt.它也不会记录stderr.
显而易见的解决方案是使用这样的东西:
powershell ".\something.exe 2>&1 | tee test.txt"
Run Code Online (Sandbox Code Playgroud)
但是,这对所有人都不适用something.exe.有些人something.exe会将其解释2>&1为一个论点而失败.正确的解决方案是只在something.exe它周围有撇号和它的开关和参数,如下所示:
powershell ".\something.exe --switch1 --switch2 … arg1 arg2 …" 2>&1 | tee test.txt
Run Code Online (Sandbox Code Playgroud)
如果您在Windows环境路径中有cygwin,则可以使用:
dir > a.txt | tail -f a.txt
Run Code Online (Sandbox Code Playgroud)
我知道这是一个非常古老的话题,但在之前的答案中,没有完整实现用 Batch 编写的实时 Tee。我下面的解决方案是一个 Batch-JScript 混合脚本,它使用 JScript 部分只是为了从管道命令中获取输出,但数据的处理是在 Batch 部分完成的。这种方法的优点是任何 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");
Run Code Online (Sandbox Code Playgroud)
我也在寻找相同的解决方案,经过一些尝试,我成功地在命令提示符中实现了这一点。这是我的解决方案:
@Echo off
for /f "Delims=" %%a IN (xyz.bat) do (
%%a > _ && type _ && type _ >> log.txt
)
@Echo on
Run Code Online (Sandbox Code Playgroud)
它甚至还可以捕获任何 PAUSE 命令。
小智 5
像这样的东西应该可以满足您的需要?
%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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
434901 次 |
| 最近记录: |