我在Windows机器上运行的批处理文件(.bat)中有一系列行,例如:
start /b prog.exe cmdparam1 cmdparam2 > test1.txt
start /b prog.exe cmdparam1 cmdparam2 > test2.txt
Run Code Online (Sandbox Code Playgroud)
有时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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1974 次 |
| 最近记录: |