I am new to windows batch programming. I need to write a loop function to execute a task if a specific error is found. Please see the code below.
I'm having a problem with the Find, which is looking for Kitchen.Error.NoRepDefinied. The script executes five times even though the find key word is not found.
Please help me identify the problem, and explain what is wrong here. Any help is appreciated. I am using 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%
)
echo !cnt!to see exactly why!cnt! neq 0? Does it make a difference to useif !cnt! gtr 0? Also, try addingecho !Kitchen.Error.NoRepDefined!to make sure that variable has its expected value in the loop.start "" /wait abc.exe > C:\Logs\App_Error.loginsteadof call abc.exe > ...And in FOR loop that... do set /a cnt=%%acould result to zero, because%%acould be a text. Then try something like... do set /a cnt=cnt+1instead. For debugging purposes try... do echo %%a