简体   繁体   English

如何在批处理脚本中将命令的输出打印到文件?

[英]How can i print output of a command to a file in Batch Script?

I am trying to call a command in batch file using "call" method and whatever is the output of that command, I want to write in a file. 我正在尝试使用“调用”方法在批处理文件中调用命令,而该命令的输出是什么,我都想写入文件中。 I went through from this link but cannot find the answer. 我通过此链接进行了搜索,但找不到答案。

I am using this command 我正在使用此命令

call %confPath% GetIniString %datFile% Keyname name >%newFile% >&1

but it creates a empty file always. 但它总是创建一个空文件。 How can i write the output of above command in the file? 如何在文件中写入以上命令的输出?

Thanks in advance. 提前致谢。

>%newFile% redirects the standard output to a file. >%newFile%将标准输出重定向到文件。 in >&1 , the 1 stands for standard output, and if no stream is specified, standard output is the default, so >&1 redirects on itself, although it was already redirected with the first command. >&11代表标准输出,如果未指定流,则默认为标准输出,因此>&1自行重定向,尽管已使用第一个命令将其重定向。 So, this is illegal and shouldn't produce a file at all. 因此,这是非法的,根本不应该产生文件。 In my tests, this just aborts with an errormessage. 在我的测试中,这只是中止并带有错误消息。

The usual idiom 2>&1 , OTOH, redirects stream 2, which is standard ERROR, to standard output, which ensures that both output and error messages end up in the file. 通常的习惯用法2>&1 OTOH将流2(标准ERROR)重定向到标准输出,以确保输出和错误消息都最终出现在文件中。

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

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