1

このような情報で継続的に書き込まれているファイルがあります

HU/gen/tcfg/target/jacinto/starter/starter_b1.cfg

32.77 34% 141.59kB/秒 0:00:00

94.64 100% 405.35kB/s 0:00:00 (xfer#60、to-check=1002/1097)

フォルダーを別のパスにコピーするのは、rsync ツールの出力です。

このファイルを読み取り、コピーされるデータの総量を計算するバッチ スクリプトを作成しようとしています。この行に焦点を当てています。

94.64 100% 405.35kB/s 0:00:00 (xfer#60、to-check=1002/1097)

数字 94.64 はファイル サイズ (バイト単位) であるため、行から「100%」の前にあるものをすべて抽出して追加する必要があると推測しています。

しかし、ファイルが同時に書き込まれている間にファイルを連続して読み取る方法がわかりません

誰か助けてくれませんか?

ありがとう

4

2 に答える 2

3

別のプロセスによって書き込まれている間にファイルを読み取る方法を示す例を次に示します。これは、プロセスが完了したかどうかを確認するための一般的なテストを追加したことを除いて、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
于 2013-02-12T16:28:43.120 に答える
2

dbenham が言ったように、それは可能ですが、バッチは最初の選択肢ではありません。
注意、サンプルは無限ループで実行されます。
ファイルの内容によっては、ブレーク条件を追加する必要があります。

ping ...プロセスが 100% の CPU 時間を消費することを回避するコマンドを追加します。

@echo off
setlocal disabledelayedexpansion
setlocal enableDelayedExpansion
< rsyncOutput.tmp (
  call :readData
)
echo ready
exit /b

:readDataX
set "line="
set /p line=
if defined line (
  for /f "tokens=1,2 delims= " %%a in ("!line!") do (
    if "%%b"=="100%%" echo Bytes %%a
  )
)
ping localhost -n 2 > nul
goto :readDataX
exit /b
于 2013-02-12T14:49:04.970 に答える