jpe解决方案要求您的父批处理知道何时启动的进程已完成,然后才能检查输出文件大小。您可以使用START/WAIT选项,但是会失去并行运行的优势。
如果另一个进程已将输出重定向到同一文件,则可以使用重定向到文件的事实。当您的父批可以重定向到他们成功,那么你知道已启动的进程已完成。
你或许应该stderr重定向到输出文件以及标准输出
@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
刚刚[删除所有文件与零长度] [1]。您可以将这些命令与“开始”命令交错。即如: [1]:http://stackoverflow.com/questions/4176962/recursively-delete-0kb-files-using-windows-cmd – jpe 2012-08-14 19:48:06
@jpe - 这似乎是唯一可能的答案,如果你想完全在DOS下处理它。您应该将其作为答案发布,以便将其标记为“正确”。 – 2012-08-14 19:52:06
Stack Overflow将我的答案改为评论,因为它太短。所以我阐述了一下:) – jpe 2012-08-14 19:57:17