我是Windows批处理编程的新手。如果发现特定错误,我需要编写一个循环函数来执行任务。请参阅下面的代码。
我遇到了Find的问题,正在寻找Kitchen.Error.NoRepDefinied。即使找不到查找关键字,脚本也会执行五次。
请帮我识别问题,并解释这里有什么问题。任何帮助表示赞赏。我使用的是Windows Server 2012 R2。
set /a x=0
:while1
if %x% leq 5 (
echo %x%
call abc.exe > C:\Logs\App_Error.log
set file=C:\Logs\App_error.log
set /a cnt=0
for /f %%a in ('type "%file%"^|find "!Kitchen.Error.NoRepDefinied!" /i /c') do set /a cnt=%%a
if !cnt! NEQ 0 (
if !x! NEQ 5 (
DEL C:\Logs\App_error.log
)
set /a x=x+1
goto :while1
)
echo "OUTSIDE LOOP"
echo The Status is %errorlevel%
call:check_file
exit /b %errorlevel%
)
答案 0 :(得分:1)
简化代码。
循环(最多5次)调用该进程。如果进程没有返回errorlevel,如果在日志文件中找不到搜索到的字符串,则保留循环。
set "logFile=c:\logs\App_Error.log"
for /l %%x in (1 1 5) do (
echo Loop %%x
> "%logFile%" call abc.exe
if not errorlevel 1 (
find "Kitchen.Error.NoRepDefinied" "%logFile%" >nul 2>&1 || goto :endLoop
)
)
:endLoop
我不确定您要获得的errorlevel
值。