windows 如何保存命令提示符的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8389652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save output from Command prompt
提问by wanderors
I know the general syntax to save the output to a text file while executing in command prompt.
我知道在命令提示符下执行时将输出保存到文本文件的一般语法。
But my need is different. I want to see it in the Command prompt window also I want to save it in text file. USually using > c:\dirlist.txt will save to text file but cant see in command prompt window.
但我的需求不一样。我想在命令提示符窗口中看到它,也想将它保存在文本文件中。通常使用 > c:\dirlist.txt 将保存到文本文件,但在命令提示符窗口中看不到。
I need to see in command prompt as well as save in text file. Any help
我需要在命令提示符下查看并保存在文本文件中。任何帮助
回答by Aacini
At first, it is relatively easy to write a TEE.BAT program. The only problem is that set /p
command (used to get the output from the command) doesn't differentiate between the end of file and an empty line. This mean that the output will be captured until the first empty line (or the end of file):
起初,编写一个 TEE.BAT 程序相对容易。唯一的问题是set /p
命令(用于从命令获取输出)不区分文件末尾和空行。这意味着输出将被捕获,直到第一个空行(或文件末尾):
@echo off
:: usage: AnyCommand | TEE filename
setlocal EnableDelayedExpansion
if exist %1 del %1
:nextLine
set line=:EOF
set /p line=
if "!line!" == ":EOF" goto :eof
echo(!line!
echo(!line!>> %1
goto nextLine
You may test that this Batch file indeed works this way:
您可以测试此批处理文件确实以这种方式工作:
tee CopyOfFile.txt < File.txt
However, when this Batch file is used in the way it was designed, it fails now and then:
但是,当按照设计的方式使用此批处理文件时,它会时不时地失败:
type File.txt | tee CopyOfFile.txt
Previous line sometimes works ok and sometimes show and store just the first line of the file. I made several tests and can not isolate the cause of the error. Perhaps someone (jeb?) could explain what is happening in this case and give a solution.
上一行有时可以正常工作,有时只显示和存储文件的第一行。我做了几次测试,但无法找出错误的原因。也许有人(jeb?)可以解释这种情况下发生的事情并给出解决方案。
回答by Jon
You want the equivalent of the tee
program for Windows. If you can use PowerShell, then you already have what you need. Otherwise, google for an implementation that works on your system.
您需要相当于tee
Windows的程序。如果您可以使用PowerShell,那么您已经拥有了所需的东西。否则,谷歌搜索适用于您系统的实现。
Alternatively, you could redirect the output to a file and observe that file with a live log viewer from another command prompt (i.e. the equivalent of tail
).
或者,您可以将输出重定向到一个文件,并使用来自另一个命令提示符的实时日志查看器(即等效于tail
)观察该文件。
回答by jeb
You can write your own tee with batch, like the one of Aacini, but without the drawbacks.
您可以使用批处理编写自己的 T 恤,就像 Aacini 的 T 恤一样,但没有缺点。
@ECHO OFF
setlocal DisableDelayedExpansion
for /F "delims=" %%A in ('findstr /n "^" ') do (
set "line=%%A"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
(echo(!line!)
(echo(!line!) > "%~1"
endlocal
)
The initially idea is from walid2me echo to screen and file in a single line
最初的想法是从 walid2me echo 到 screen 和 file in a single line
回答by T3RR0R
The following is another approach for displaying and Logging command output:
以下是显示和记录命令输出的另一种方法:
@Echo off & CD "%~dp0"
For /F "UsebackQ Tokens=* Delims=" %%A in (`"%~1"`) do (
>>Outputfile.txt Echo(%%A
Echo(%%A
)
Goto :EOF