Redirect DOS output only on exit

I have a series of lines in a batch file (.bat) running on a Windows machine, for example:

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

Sometimes proj.exe returns nothing (empty) instead of useful data. In such cases, I don’t want to generate a text file, is it something easy to do on the batch file side? The current behavior is that a text file is always created, in the case of empty output - just an empty file.

+5
source share
2 answers

The jpe solution requires your parent package to know when the running processes have completed before it can check the size of the output files. You can use the START / WAIT option, but then you lose the advantage of parallel operation.

, , . , , .

, 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
+4

. : , start /WAIT prog.exe, script progwrapper.bat prog.exe:

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

script:

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

.

prog.exe , start /B /WAIT prog.exe progwrapper.bat.

+2

All Articles