批处理脚本读取连续写入的文件 [英] Batch script read a file that's continously written

查看:90
本文介绍了批处理脚本读取连续写入的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件不断被写着这样的信息

I have a file that's being continuously written with information like this

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

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

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

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

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

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

rsync工具的输出,它将文件夹复制到另一个路径

It is the output of rsync tool, that copies a folder to another path

我正在尝试编写一个批处理脚本,该脚本读取此文件并计算要复制的数据总量,重点是此行

I'm trying to write a batch script that reads this file and calculates the total amount of data being copied, focusing on this line

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

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

数字94.64是文件大小(以字节为单位),所以我猜我应该从该行中提取"100%"之前的内容并将其添加

the number 94.64 being the file size in bytes, so I'm guessing I should extract whatever is before "100%" from the line and add it

但是我不知道如何在同时写入文件的同时连续读取文件

But I don't know how to continuously read the file while it's being written at the same time

有人可以帮忙吗?

谢谢

推荐答案

下面是一个示例,该示例显示了如何在文件被另一个进程写入时读取文件.这与jeb显示的内容非常相似,除了我添加了通用测试以查看过程是否完成.它假定过程在整个执行过程中都对文件保持锁定,还假定20个连续的空行指示文件末尾,否则我们正在等待过程中的更多输出.空行阈值20可以设置为任何适合您的数字.

Here is an example that shows how to read a file while it is being written by another process. This is very similar to what jeb shows, except I have added a generic test to see if the process is complete. It assumes the process maintains a lock on the file throughout the entire execution, and it also assumes 20 consecutive empty lines indicates either the end of file, or else we are waiting for more output from the process. The 20 empty line threshold could be set to any number that works for you.

如果该进程将部分行写入文件,则此解决方案可能无法正常工作.我认为,如果该过程始终在单个操作中完整写入每一行,那将是可靠的.此外,这些行的长度必须小于或等于1021个字节,并且必须以回车换行符终止.

This solution may not work properly if the process writes partial lines to the file. I believe it is reliable if the the process always writes each line in its entirety in a single operation. Also, the lines must be less than or equal to 1021 bytes long, and they must be terminated by carriage return, linefeed.

@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

这篇关于批处理脚本读取连续写入的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆