Windows批处理:如何将cmd输出写入文件? [英] Windows batch: how to write cmd output to a file?
问题描述
我需要将批处理运行时cmd窗口中出现的行打印到文本文件中.例如,我有以下批处理脚本:
I need to print into a text file the lines that appear in the cmd window when the batch is running. For instance, I have the following batch script:
copy D:\aaa.txt D:\bbb.txt
启动批处理文件时,cmd窗口显示以下几行(对不起,我的Windows是法语):
When I launch the batch file, the cmd window displays the following lines (sorry my Windows is in french):
D:\>copy D:\aaa.txt D:\bbb.txt
1 fichier(s) copié(s)
我想自动将这两行写到文本文件中,以检查更复杂的批处理文件的执行情况并跟踪错误.
I would like to automatically write these 2 lines in a text file in order to check the execution of more complex batch files and to track errors.
推荐答案
将批处理命令的输出重定向到文件log.txt
:
Redirect the output of a batch command into a file log.txt
:
D:\>copy D:\aaa.txt D:\bbb.txt >log.txt
将输出追加到文件
copy D:\aaa.txt D:\bbb.txt >>log.txt
请注意,您可能想取消确认是否覆盖目标文件.使用/Y
选项执行此操作(但要注意风险):
Note that you might want to suppress the confirmation whether or not to overwrite the target file. Use the /Y
option to do so (but be aware of the risks):
copy /Y D:\aaa.txt D:\bbb.txt >>log.txt
还请注意,发出的命令行不是输出的一部分.使用echo
命令将其写入:
Also note that the issued command line is not part of the output. Write it using the echo
command:
echo copy /Y D:\aaa.txt D:\bbb.txt >>log.txt
copy /Y D:\aaa.txt D:\bbb.txt >>log.txt
这篇关于Windows批处理:如何将cmd输出写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!