2013-02-12 44 views
0

我的影片正在不断像这样批处理脚本读取真实汽车无书面

HU/GEN/TCFG /目标/哈辛托/起动/ starter_b1.cfg

32.77 34信息写入一个文件的文件%141.59kB/s的0点00分00秒

94.64 100%405.35kB/s的0点00分00秒(#XFER 60,以检查=一千○九十七分之一千○二)

它的输出rsync工具,即copie SA夹到另一个路径

我试图写一个批处理脚本,读取该文件,并计算被复制的数据的总量,着眼于这条线

94.64 100%405.35kB/S 0 :00:00(XFER#60,以检查=一千○九十七分之一千○二)

数94.64为以字节为单位的文件的大小,所以我猜我应该提取从“100%”前任何是并添加它

但我不知道如何连续阅读文件,同时它被写入

有人可以帮忙吗?

谢谢

+1

不可能从批处理文件AFAIK。如果你可以将rsync工具的输出重定向到批处理文件,那么它应该是可行的。 – Jon 2013-02-12 09:53:36

+1

我会使用'tail -v' from cygwin – 2013-02-12 09:54:54

+0

@Jon我已经使用批处理脚本,在另一个cmd窗口中启动rsync,并且rsync输出被重定向到一个txt文件。在我的批处理脚本中,在启动rsync的下一行中,我想读取txt文件并计算复制的数据量。原因是因为我已经有了文件夹的总大小(大约4GB),并且我想在控制台输出上写入多少日期将被复制。可能吗? – 2013-02-12 10:21:33

回答

1

像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 
1

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

如果该过程将部分行写入文件,此解决方案可能无法正常工作。如果这个过程总是在单一操作中完成每行的完整写入,我相信这是可靠的。此外,这些行必须小于或等于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