gpt4 book ai didi

batch-file - 仅当有输出时才重定向 DOS 输出

转载 作者:行者123 更新时间:2023-12-04 18:47:26 26 4
gpt4 key购买 nike

我在 Windows 机器上运行的批处理文件 (.bat) 中有一系列行,例如:

start /b prog.exe cmdparam1 cmdparam2 > test1.txt
start /b prog.exe cmdparam1 cmdparam2 > test2.txt

有时 proj.exe 不返回任何内容(空)而不是有用的数据。在这些情况下,我不想生成文本文件,这在批处理文件方面容易实现吗?当前的行为是始终创建一个文本文件,在空输出的情况下,它只是一个空白文件。

最佳答案

jpe 解决方案要求您的父批处理在检查输出文件大小之前知道启动的进程何时完成。您可以使用 START/WAIT 选项,但是这样您就失去了并行运行的优势。

如果另一个进程已经将输出重定向到同一个文件,则您可以使用重定向到文件将失败的事实。当您的父批次可以成功重定向到它们时,您就知道启动的进程已全部完成。

您可能应该将 stderr 重定向到输出文件以及 stdout

@echo off

::start the processes and redirect the output to the ouptut files
start /b "" cmd /c prog.exe cmdparam1 cmdparam2 >test1.txt 2>&1
start /b "" cmd /c prog.exe cmdparam1 cmdparam2 >test2.txt 2>&1

::define the output files (must match the redirections above)
set files="test1.txt" "test2.txt"

:waitUntilFinished
:: Verify that this parent script can redirect an unused file handle to the
:: output file (append mode). Loop back if the test fails for any output file.
:: Use ping to introduce a delay so that the CPU is not inundated.
>nul 2>nul ping -n 2 ::1
for %%F in (%files%) do (
9>>%%F (
rem
)
) 2>nul || goto :waitUntilFinished

::Delete 0 length output files
for %%F in (%files%) do if %%~zF==0 del %%F

关于batch-file - 仅当有输出时才重定向 DOS 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11959650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com