別のプロセスによって書き込まれている間にファイルを読み取る方法を示す例を次に示します。これは、プロセスが完了したかどうかを確認するための一般的なテストを追加したことを除いて、jeb が示すものと非常によく似ています。プロセスが実行全体を通してファイルのロックを維持していると想定し、20 行の連続する空行がファイルの終わりを示しているか、プロセスからのさらなる出力を待っていることも想定しています。20 の空行のしきい値は、適切な数値に設定できます。
プロセスが部分的な行をファイルに書き込む場合、このソリューションは正しく機能しない可能性があります。プロセスが常に1回の操作で各行全体を書き込む場合、信頼できると思います。また、行の長さは 1021 バイト以下である必要があり、キャリッジ リターン、ラインフィードで終了する必要があります。
@echo off
setlocal enableDelayedExpansion
set "file=test.txt"
set emptyCount=0
del "%file%
:: For this test, I will start an asynchronous process in a new window that
:: writes a directory listing to an output file 3 times, with a 5 second pause
:: between each listing.
start "" cmd /c "(for /l %%N in (1 1 3) do @dir & ping localhost -n 6 >nul) >"%file%""
:wait for the output file to be created and locked
if not exist "%file%" goto :wait
:: Now read and process the file, as it is being written.
call :read <"%file%"
exit /b
:read
set "ln="
set /p "ln="
if defined ln (
REM Process non-empty line here - test if correct line, extract size, and add
REM I will simply echo the line instead
echo(!ln!
REM Reset emptyCount for end of file test and read next line
set /a emptyCount=0
goto :read
) else ( REM End of file test
REM Count how many consecutive empty lines have been read.
set /a emptyCount+=1
REM Assume that 20 consecutive empty lines signifies either end of file
REM or else waiting for output. If haven't reached 20 consectutive
REM empty lines, then read next line.
if !emptyCount! lss 20 goto :read
REM Test if output file is still locked by attempting to redirect an unused
REM stream to the file in append mode. If the redirect fails, then the file
REM is still locked, meaning we are waiting for the process to finish and have
REM not reached the end. So wait 1 sec so as not to consume 100% of CPU, then
REM reset count and try to read the next line again.
REM If the file is not locked, then we are finished, so simply fall through
REM and exit.
(echo off 9>>"%file%")2>nul || (
set emptyCount=0
ping localhost -n 2 >nul
goto :read
)
)
exit /b