2012-07-11 37 views
1

我有一个进程,每天计划的批处理文件启动。如果出现错误,我需要内置错误处理以重新启动进程。所有的作品大部分时间都很棒,但我每个月都会遇到一次不可避免的错误。该进程不会向bat文件输出错误级别,因此我需要能够解析输出文件以确定进程是否需要重新启动。BAT:解析错误处理输出文件

我尝试使用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. 
+0

所以,你只是想在输出文件中的第12行? – 2012-07-11 16:39:50

+0

这是一种方法来处理它,如果我能得到我应该设置。 – retroActive 2012-07-11 16:55:46

回答

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 
) 
+0

+1。整齐。但你怎么知道这是唯一可能的错误信息? – 2012-07-11 20:49:33

+0

@EitanT - 我不明白:)但只要所有可能的错误消息是已知的(或至少一个不同的片段),那么FINDSTR可以用于多个/ C参数,每个消息一个。或者FINDSTR可以用于正则表达式来匹配一些不同的错误消息模式。 – dbenham 2012-07-11 21:09:42

+0

谢谢dbenham,这个工作很棒! – retroActive 2012-07-11 21:18:08

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)