BAT:解析错误处理的输出文件

时间:2012-07-11 16:37:23

标签: error-handling batch-file for-loop

我有一个每天由计划批处理文件启动的进程。如果出现错误,我需要内置错误处理来重启进程。一切都很有效,但我每个月都会有一次超时错误,这是不可避免的。该进程不会向bat文件输出errorlevel,因此我需要能够解析输出文件以确定进程是否需要重新启动。

我尝试使用FOR /F函数将第12行的内容作为变量传递给IF语句但我没有成功。我显然可以跳到第12行,但后来我处理其余行的标记。有没有人有任何我可以尝试的建议?

一切顺利时输出文件:

(为可读性添加了行号)

1  Pricing Script
2   
3  ________________________________________________________________________ 
4
5  Retrieve Prices
6
7  Date of price file: 070912
8  Regular only 
9  Connecting to server intdata.com
10 TCP/IP connection established
11  
12 TySymb          Interactive Data
+400 more lines

出现错误时输出文件:

1  Pricing Script
2  
3  ________________________________________________________________________ 
4 
5  Retrieve Prices
6  
7  Date of price file: 071012
8  Regular only 
9  Connecting to server intdata.com
10 TCP/IP connection established
11 Time Out
12 General Time Out. The User ID and/or Password might be incorrect.

2 个答案:

答案 0 :(得分:2)

我只是使用FIND或FINDSTR在输出中查找错误消息。我不担心行号。

find "General Time Out. The User ID and/or Password might be incorrect." "yourFile.log" && (
  echo Timeout occurred, you must put code here to restart
)

findstr /c:"General Time Out. The User ID and/or Password might be incorrect." "yourFile.log" && (
  echo Timeout occurred, you must put code here to restart
)

答案 1 :(得分:0)

这将仅针对给定字符串测试文件的第12行:

@echo off
for /f "skip=11 tokens=*" %%i in (your_log_filename) do (
 echo %%i | find "General Time Out" >nul
 goto check
)

:check
if errorlevel 1 (echo No Timeout occured!) else (echo Timeout occured)