2

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%
)
2
  • Nice rookie effort. +1 for putting in the work before asking for help! Have you tried echo !cnt! to see exactly why !cnt! neq 0? Does it make a difference to use if !cnt! gtr 0? Also, try adding echo !Kitchen.Error.NoRepDefined! to make sure that variable has its expected value in the loop. Commented Nov 24, 2014 at 16:52
  • To wait until your abc.exe fills and closes the log so you could use it properly: start "" /wait abc.exe > C:\Logs\App_Error.log instead of call abc.exe > ... And in FOR loop that ... do set /a cnt=%%a could result to zero, because %%a could be a text. Then try something like ... do set /a cnt=cnt+1 instead. For debugging purposes try ... do echo %%a Commented Nov 24, 2014 at 23:03

1 Answer 1

1

Simplify the code.

Loop (up to 5 times) calling the process. If the process does not return errorlevel, if in the log file the searched string is not found, then leave the loop.

    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

I'm not sure about the errorlevel value you are trying to get.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for help. This is really working. I also need a help in the code. Sometimes my abc.exe fails with database connectivity errors which is not logged when the application runs. Then I figured that I need to look for the errorlevel 0 and string match for success. Now I would want to add one more condition in the code like Find the string as well as the errorlevel %ERRORLEVEL% should be equal to zero. How do i add the second condition in the loop
Great thanks. I did a similar one but I have used if %ERRORLEVEL% NEQ 0. I still was not getting the expected answer i want. The loop ran for 5 times and the condition was not satisfied.Now hope i have understood the syntax and will use it for future .Thanks a ton. but my doubt is why i was not able to use %ERRORLEVEL% in the if condition and almost spent days to understand this. Thank you so much. Marking you answer as accepted :)
@Karthi, in batch files variable reads inside a block of code are replaced with the value in the variable before the code starts to execute, so, you can not see the changed value in %errorlevel%. Search SO for delayed expansion in batch files.
Yeah I used setlocal enabledelayedexpansion in my batch file.I used to send a database mail based on the stauts. When I used delayed expansion I get a db error saying that Login failed for user. Then I removed setlocal enabledelayedexpansion.
How does the pipe || works here. If the found value is zero it just passes the command execution to the goto label?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.