2015-09-05 57 views
1

我正在嘗試編寫一個批處理文件,該文件在找到文本字符串時會創建一個事件,如果找不到字符串,則會創建另一個事件。我至今是:根據找到的文本創建事件的批處理文件

rem *************************************************************************************************************************************************************** 
rem * If the log file contains the 'copied' word log an event 
rem *************************************************************************************************************************************************************** 

find /c "copied" C:\Richards Folders\backup service\create events and logs\test backup log\backup.log 
if errorlevel 1 goto ONE 
if errorlevel 0 goto ZERO 

:ZERO 
eventcreate /L Application /T Information /SO "BackUp Service" /ID 900 /D "Copied in backing up files to the back-up store. For more details look in C:\Richards Folders\backup service\create events and logs\test backup log." 
goto END 

:ONE 
eventcreate /L Application /T Error /SO "BackUp Service" /ID 901 /D "Error in backing up files to the back-up store. For more details look in C:\Richards Folders\backup service\create events and logs\test backup log." 
:END 

顯然,我做錯了什麼,因爲它要麼始終報告成功,甚至當我告訴腳本尋找一個胡說八道的文本,或者當正確的文本存在時,它報告失敗。

我的代碼有什麼問題?

回答

0

試試這個代碼:

rem *************************************************************************************************************************************************************** 
rem * If the log file contains the 'copied' word log an event 
rem *************************************************************************************************************************************************************** 

%SystemRoot%\System32\find.exe /c "copied" "C:\Richards Folders\backup service\create events and logs\test backup log\backup.log" 
if errorlevel 1 goto NotFound 

%SystemRoot%\System32\eventcreate.exe /L Application /T Information /SO "BackUp Service" /ID 900 /D "Copied in backing up files to the back-up store. For more details look in C:\Richards Folders\backup service\create events and logs\test backup log." 
goto :EOF 

:NotFound 
%SystemRoot%\System32\eventcreate.exe /L Application /T Error /SO "BackUp Service" /ID 901 /D "Error in backing up files to the back-up store. For more details look in C:\Richards Folders\backup service\create events and logs\test backup log." 

日誌文件的名稱以及它的路徑包含空格。因此,您必須將帶有路徑的日誌文件名用雙引號引起來。

另請參見Microsoft支持文章Testing for a Specific Error Level in Batch Files

條件if errorlevel 0幾乎總是如此,因爲它意味着大於或等於0,並且幾乎沒有應用程序退出並返回負值。

0

這是另一種方法。

您的錯誤是日誌文件路徑\名稱需要雙引號。

find /c "copied" < "C:\Richards Folders\backup service\create events and logs\test backup log\backup.log" 

if not errorlevel 1 (
eventcreate /L Application /T Information /SO "BackUp Service" /ID 900 /D "Copied in backing up files to the back-up store. For more details look in C:\Richards Folders\backup service\create events and logs\test backup log." 
) else (
eventcreate /L Application /T Error /SO "BackUp Service" /ID 901 /D "Error in backing up files to the back-up store. For more details look in C:\Richards Folders\backup service\create events and logs\test backup log." 
) 
相關問題