5

Windows マシンで実行されているバッチ ファイル (.bat) に一連の行があります。

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

場合によっては、proj.exe が有用なデータではなく何も返さない (空) ことがあります。そのような場合、テキスト ファイルを生成したくないのですが、これはバッチ ファイル側で簡単に実行できますか? 現在の動作では、常にテキスト ファイルが作成されます。空の出力の場合、それは単なる空のファイルです。

4

2 に答える 2

4

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
于 2012-08-14T20:48:50.700 に答える
2

長さがゼロのすべてのファイルを削除するだけです。start編集: が終了するのを待つ前に /WAIT フラグが返されないという事実に対応するprog.exeために、次のラッパースクリプトを作成できprogwrapper.batますprog.exe:

prog.exe "%1" "%2" > "%3"
if %~z3==0 del "%3"

次に、マスター スクリプトからラッパーを呼び出します。

start /b progwrapper.bat cmdparam1 cmdparam2 > test1.txt
start /b progwrapper.bat cmdparam1 cmdparam2 > test2.txt

prog.exe が GUI アプリの場合、progwrapper.bat に が必要ですstart /B /WAIT prog.exe

于 2012-08-14T19:56:05.173 に答える