简体   繁体   English

批处理:如何将命令的输出重定向到文件

[英]batch: how to redirect the output of a command to a file

ping google.com >> ping.txt

that dosent work if i put it in a batch file and run the file 如果我将其放在批处理文件中并运行该文件,则可以正常工作

this is the output of ping.txt: 这是ping.txt的输出:

C:\Users\User\Desktop>ping google.com  1>>ping.txt  
C:\Users\User\Desktop>ping google.com  1>>ping.txt  
. 
. 
.

You're doing something strange with pipes. 您正在用管道做一些奇怪的事情。

This (from command line) will append the ping result to ping.txt file. (从命令行)这会将ping结果附加ping.txt文件。

ping www.google.com >> ping.txt

Now create a batch file ( testping.bat , for example) and put the same line inside it. 现在创建一个批处理文件(例如testping.bat ),并将同一行放入其中。 If you run the batch file with following command you'll get the same result. 如果使用以下命令运行批处理文件,您将获得相同的结果。

testping

If you redirect the output of the batch itself then what you'll get is the echo of the batch file commands! 如果您重定向批处理本身的输出,那么您将获得批处理文件命令的回显! This is why this will not work as expected: 这就是为什么它不能按预期工作的原因:

testping >> ping.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM