A few improvements to jeb's answer.
There is no need for an extra lock file. The queue.txt file can serve as its own lock.
Also, the innermost block must be guaranteed to exit with SUCCESS (errorlevel 0) so that the retry only occurs when the redirection failed. (CALL )
(note the trailing space) is a totally non-intuitive, yet extremely efficient way to clear any error. Not needed here, but (CALL)
(note the lack of space) is an efficient way to set an error.
:lockedAppend
2>nul (
>>queue.txt (
REM Do any amount of processing within this block.
REM All stdout will safely be appended to the queue
REM You could even call out to a subroutine, or another batch file
echo Any text you want
REM The CALL below ensures that the block exits with ERRORLEVEL 0 - success
(call )
)
)||goto :lockedAppend
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…