Windows batch: how to write cmd output to a file?

混江龙づ霸主 提交于 2019-12-12 02:47:21

问题


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

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.


回答1:


Redirect the output of a batch command into a file log.txt:

D:\>copy D:\aaa.txt D:\bbb.txt >log.txt

Append the output to the file

copy D:\aaa.txt D:\bbb.txt >>log.txt

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

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


来源:https://stackoverflow.com/questions/33438065/windows-batch-how-to-write-cmd-output-to-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!