将所有stdout重定向到文件?

时间:2017-11-22 14:00:24

标签: windows batch-file cmd pipe

非常简单。我想要类似下面的例子:

echo "Hello world. " > some/file.txt
echo "This is " > some/file.txt
echo "some awesome " > some/file.txt
echo "text!!!" > some/file.txt

stdOutFile = "some/file.txt"
echo "Hello world. "
echo "This is "
echo "some awesome "
echo "text!!!"

4 个答案:

答案 0 :(得分:3)

您可以使用以下格式:

@Echo Off
Set "stdOutFile=some\file.txt"
(   Echo Hello world. 
    Echo This is 
    Echo some awesome 
    Echo text!!!
)>"%stdOutFile%"

如果您打算将它们全部放在同一行,那么也许:

@Echo Off
Set "stdOutFile=some\file.txt"
(   For %%A In (
        "Hello world. "
        "This is "
        "some awesome "
        "text!!!"
    ) Do Set/P =%%~A<Nul
)>"%stdOutFile%"

<强> [编辑]

你甚至可以使用以下想法:

@Echo Off
Set "stdOutFile=somefile.txt"
Set "str=Hello world."
Set "str=%str% This is"
Set "str=%str% some awesome"
Set "str=%str% text!!!"
(Echo %str%)>"%stdOutFile%"

如果您的批处理文件不是特别具有交互性,那么您可以按照其他答案中的建议运行批处理文件:

<强> mybatchfile.cmd

@Echo Off
Echo Hello world. 
Echo This is 
Echo some awesome 
Echo text!!!

的StdOut:

"mybatchfile.cmd">"some\file.txt"

您也可以将任何错误定向到同一个文件。

StdOut&amp;标准错误:

"mybatchfile.cmd">"some\file.txt" 2>&1

答案 1 :(得分:1)

如果你不能把echo代码放到一个单独的.bat脚本中,一种让它更“适合”的方法就是用echo命令重定向。

SET "stdOutFile=some/file.txt"
IF EXIST "%stdOutFile%" (DEL "%stdOutFile%")

echo>>"%stdOutFile%" "Hello world. "
echo>>"%stdOutFile%" "This is "
echo>>"%stdOutFile%" "some awesome "
echo>>"%stdOutFile%" "text!!!"

答案 2 :(得分:0)

将其放在.bat - 文件中:

@echo off
echo "Hello world. "
echo "This is "
echo "some awesome "
echo "text!!!"

然后运行如下:

my.bat > output.txt

答案 3 :(得分:0)

添加

@echo off

在批处理文件的开头,然后执行

.\batchfile >outputfilename