2012-07-11 121 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)