批处理:计算从时间戳开始经过的时间

时间:2016-09-21 14:40:45

标签: batch-file

伙计们,我有一个任务来分析像这样的日志文件:

12:03:11 Testing Import (test #240.)
12:10:47 Testing Searches (test #241.)
12:14:39 Testing Default notifications (test #242.)
12:20:05 Testing Inventory list monitor (test #243.)
12:34:31 Testing Reports from console (test #244.)
12:40:05 Testing Users and user groups (test #245.)
12:43:33      ERROR: 1024 characters in field Name, warning not shown!
12:48:13 Testing RDE (test #246.)

我的工作是计算每次测试所用的时间并将其写入另一个txt文件。当然像这样的线

12:43:33 ERROR: 1024 characters in field Name, warning not shown!

不应该被考虑在内。所以这个字符串(test #可以作为某种标识符。优先语言是批量的,但我的经验非常有限,所以任何帮助都是有用的。谢谢!

1 个答案:

答案 0 :(得分:2)

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q39619700.txt"
SET "outfile=%destdir%\outfile.txt"
SET "starthh="
(
FOR /f "tokens=1-3*delims=: " %%a IN (
 'findstr /L /c:"(test #" "%filename1%"') DO (
 IF DEFINED starthh (
  CALL :report 1%%a 1%%b 1%%c
 ) ELSE (
  SET /a starthh=1%%a&SET /a startmm=1%%b&SET /a startss=1%%c
 )
 SET "event=%%d"
)
)>"%outfile%"

GOTO :EOF

:report
SET /a starthh=((%1-starthh)*3600)+((%2-startmm)*60)+%3-startss
IF %starthh% lss 0 SET /a starthh+=3600*24
:: If in seconds
ECHO %starthh% seconds FOR %event%
:: If in hh:mm:ss
SET /a startss=100+(starthh %% 60)
SET /a startmm=100+((starthh-startss+100) %% 3600) / 60
SET /a starthh=100+(starthh/3600)
ECHO %starthh:~-2%:%startmm:~-2%:%startss:~-2% FOR %event%
SET /a starthh=%1
SET /a startmm=%2
SET /a startss=%3
GOTO :eof

您需要更改sourcedirdestdir的设置以适合您的具体情况。

我使用了一个名为q39619700.txt的文件,其中包含我的测试数据。

生成定义为%outfile%

的文件

对于日志中的每个条目,使用findstring进行过滤,仅查找包含(test #的行,记录开始时间元素和事件文本。

处理只是从旧的时间元素中减去新的时间元素,计算结果并用计算的经过时间再现旧的事件文本;然后重新设置最新一行的时间元素。注意在时间元素字符串之前使用1来克服前导零 - 八进制问题。

相关问题