【问题标题】:Capture console output of called application in calling batch file在调用批处理文件中捕获被调用应用程序的控制台输出
【发布时间】:2020-08-14 00:42:50
【问题描述】:

我有一个批处理文件,假设一个接一个地启动 10 个应用程序。

它等待启动的应用程序完成,然后继续下一个。这里没有问题。

但是我无法(在批处理文件中)捕获 10 个被调用应用程序中的每一个生成的控制台输出。

下面是我要说的:

start /wait ./TestApp1/Debug/TestApp1.exe
start /wait ./TestApp2/Debug/TestApp2.exe
start /wait ./TestApp3/Debug/TestApp3.exe
start /wait ./TestApp4/Debug/TestApp4.exe
start /wait ./TestApp5/Debug/TestApp5.exe
start /wait ./TestApp6/Debug/TestApp6.exe
start /wait ./TestApp7/Debug/TestApp7.exe
start /wait ./TestApp8/Debug/TestApp8.exe
start /wait ./TestApp9/Debug/TestApp9.exe
start /wait ./TestApp10/Debug/TestApp10.exe

假设每个 TestApp 生成一些输出。 我想将它们全部合并到一个文本文件中。 关于如何实现这一点的任何指示?

【问题讨论】:

  • 用(括号)包围整个执行过程,然后使用重定向将输出捕获到文件中。 (all your exeecutables)>logfile.txt
  • @Squashman 你是这个意思吗? (start /wait ./TestApp1/Debug/TestApp1.exe start /wait ./TestApp2/Debug/TestApp2.exe start /wait ./TestApp3/Debug/TestApp3.exe start /wait ./TestApp4/Debug/TestApp4.exe start /wait ./TestApp5/Debug/TestApp5.exe start /wait ./TestApp6/Debug/TestApp6.exe start /wait ./TestApp7/Debug/TestApp7.exe start /wait ./TestApp8/Debug/TestApp8.exe start /wait ./TestApp9/Debug/TestApp9.exe start /wait ./TestApp10/Debug/TestApp10.exe) > logfile.txt?
  • 从 cmd.exe 或其他批处理文件中,您可以使用 Call "M:\yBatch\file.cmd">"logfile.txt",如果您还想包含任何错误,请将其更改为 Call "M:\yBatch\file.cmd">"logfile.txt" 2>&1

标签: batch-file logging scripting


【解决方案1】:

我建议你首先尝试不使用Start 命令:

@(
    "TestApp1\Debug\TestApp1.exe"
    "TestApp2\Debug\TestApp2.exe"
    "TestApp3\Debug\TestApp3.exe"
    "TestApp4\Debug\TestApp4.exe"
    "TestApp5\Debug\TestApp5.exe"
    "TestApp6\Debug\TestApp6.exe"
    "TestApp7\Debug\TestApp7.exe"
    "TestApp8\Debug\TestApp8.exe"
    "TestApp9\Debug\TestApp9.exe"
    "TestApp10\Debug\TestApp10.exe"
) > "logfile.txt"

如果你真的需要那个Start 命令,那么类似:

@(
    Start "" /Wait "TestApp1\Debug\TestApp1.exe"
    Start "" /Wait "TestApp2\Debug\TestApp2.exe"
    Start "" /Wait "TestApp3\Debug\TestApp3.exe"
    Start "" /Wait "TestApp4\Debug\TestApp4.exe"
    Start "" /Wait "TestApp5\Debug\TestApp5.exe"
    Start "" /Wait "TestApp6\Debug\TestApp6.exe"
    Start "" /Wait "TestApp7\Debug\TestApp7.exe"
    Start "" /Wait "TestApp8\Debug\TestApp8.exe"
    Start "" /Wait "TestApp9\Debug\TestApp9.exe"
    Start "" /Wait "TestApp10\Debug\TestApp10.exe"
) > "logfile.txt"

如果您的应用程序需要使用Start,那么建议您查看其使用信息,(可能还需要/B 等选项)

请注意,我已经包含了双引号,这样脚本就可以用作其他人的模板,而不必担心有问题的字符,而且我还为 Windows 使用了正确的路径分隔符,即反斜杠。

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2015-08-14
    相关资源
    最近更新 更多