Windows 命令提示符记录到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14500616/
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
Windows command prompt log to a file
提问by Luis Andrés García
I'd like to easily save the log of the Windows command prompt into a file. Is there any alternative to select everything and right-click on copy?
我想轻松地将 Windows 命令提示符的日志保存到一个文件中。有没有其他选择可以选择所有内容并右键单击复制?
回答by Bali C
You can redirect the output of a cmd prompt to a file using >
or >>
to append to a file.
您可以使用>
或>>
附加到文件将cmd 提示的输出重定向到文件。
i.e.
IE
echo Hello World >C:\output.txt
echo Hello again! >>C:\output.txt
or
或者
mybatchfile.bat >C:\output.txt
Note that using >
will automatically overwrite the file if it already exists.
请注意,>
如果文件已存在,则 using将自动覆盖该文件。
You also have the option of redirecting stdin, stdout and stderr.
您还可以选择重定向标准输入、标准输出和标准错误。
See herefor a complete list of options.
有关完整的选项列表,请参见此处。
回答by Mr.Hunt
First method
第一种方法
For Windows 7 and above users, Windows PowerShell give you this option. Users with windows version less than 7 can download PowerShell online and install it.
对于 Windows 7 及更高版本的用户,Windows PowerShell 为您提供此选项。windows 7以下版本的用户可以在线下载PowerShell并安装。
Steps:
脚步:
type PowerShell in search area and click on "Windows PowerShell"
If you have a .bat (batch) file go to step 3 OR copy your commands to a file and save it with .bat extension (e.g. file.bat)
run the .bat file with following command
PS (location)> <path to bat file>/file.bat | Tee-Object -file log.txt
在搜索区域中键入 PowerShell 并单击“Windows PowerShell”
如果您有 .bat(批处理)文件,请转到第 3 步,或者将您的命令复制到一个文件中并使用 .bat 扩展名(例如 file.bat)保存
使用以下命令运行 .bat 文件
PS (location)> <path to bat file>/file.bat | Tee-Object -file log.txt
This will generate a log.txt file with all command prompt output in it. Advantage is that you can also the output on command prompt.
这将生成一个 log.txt 文件,其中包含所有命令提示符输出。优点是您还可以在命令提示符下输出。
Second method
第二种方法
You can use file redirection (>, >>) as suggest by Bali Cabove.
您可以按照上面Bali C 的建议使用文件重定向(>、>>)。
I will recommend first method if you have lots of commands to run or a script to run. I will recommend last method if there is only few commands to run.
如果您有很多命令要运行或脚本要运行,我会推荐第一种方法。如果只有几个命令要运行,我会推荐最后一种方法。
回答by oscar_ceja
In cmd when you use > or >> the output will be only written on the file. Is it possible to see the output in the cmd windows and also save it in a file. Something similar if you use teraterm, when you can start saving all the log in a file meanwhile you use the console and view it (only for ssh, telnet and serial).
在 cmd 中,当您使用 > 或 >> 时,输出将仅写入文件。是否可以在 cmd 窗口中查看输出并将其保存在文件中。如果您使用 teraterm,则类似,当您可以开始将所有日志保存在文件中时,您可以使用控制台查看它(仅适用于 ssh、telnet 和串行)。