0

When I run a command from the CMD directly it works perfectly fine but when run the same command in a batch file (or Jenkins using batch) it gives me a different result and I dont understand why.

To explain it simple, I'm running command below to search for a literal string within a log file:

findstr /C:"MY STRING WITH A %variable%" M:\Logs\output.log

If I check the %ERRORLEVEL% of the result it shows the expected values (0 = found the string or 1 = didnt find it)

However, when I run the same line from a batch file, even from Jenkins, the result is always 0, even though the string is not present in the log, the %ERRORLEVEL% is always 0.

This is the portion of the batch file which includes the command:

if %COUNTER% ==1 ( 
    if not exist M:\Logs\current_bak (
        ROBOCOPY "M:\Logs\tkcurrent" "M:\Logs\tkcurrent_bak"
        REN "M:\Logs\current" "M:\Logs\current_bak"
        MKDIR M:\Logs\current
        echo Folders have been backed up
    ) else (
        echo Back up folders are already in place )

    findstr /C:"MY STRING WITH A %variable%" M:\Logs\output.log
    if %ERRORLEVEL% == 0 (
        echo Process has already being kicked off for the files with date %YYMMDD%, skipping it...
        echo Download and backup compleated
        exit /b 0 )
    else (
        echo Triggering next Jenkins Job:  
        curl -I http://<user>:<token>@remoteserver.domain.com:<port>/job/Hello_World/build?token=hello_world
        exit /b 0 )
)

Has someone experienced this in the past or can guide me better on what I'm doing wrong or not understanding?

Thanks a lot!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Avila
  • 3
  • 4
  • Is `findsrt` a typo of `findstr`? The reason your `else` is not triggering is a basic syntactical error. Try putting the closing parenthesis of the `if` statement on the same line as the `else`. – Nico Nekoru Jun 02 '20 at 17:44
  • Sorry that was a typo, fixing it now! Thanks! – Avila Jun 02 '20 at 17:57
  • 2
    `errorlevel` either needs [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) or `if %ERRORLEVEL%` has to be replaced by `if not errorlevel 1` – Stephan Jun 02 '20 at 18:01

1 Answers1

0

Your error is a basic syntactical error that I've made plenty of times as well :P. The issue is in your else statement which is a peculiar issue, but what happens is that the else only will get processed if it is on the same line as the if statement.


PROOF OF CONCEPT

If I have the if statement and else on different lines like so:

@echo off
echo test
if errorlevel 1 (echo here) 
else (echo not there)
pause

This will have the error:

'else' is not recognized as an internal or external command,
operable program or batch file.

However if I change the code a tiny bit so that the else is on the same line as the if, like so:

@echo off
echo test
if errorlevel 1 (echo here
) else (echo not there)
pause

It will not have an error and output

test
not there


Your code done correctly would be:
if %COUNTER%==1 ( 
    if not exist M:\Logs\current_bak (
        ROBOCOPY "M:\Logs\tkcurrent" "M:\Logs\tkcurrent_bak"
        REN "M:\Logs\current" "M:\Logs\current_bak"
        MKDIR M:\Logs\current
        echo Folders have been backed up
    ) else (
        echo Back up folders are already in place )

    findstr /C:"MY STRING WITH A %variable%" M:\Logs\output.log
    if %ERRORLEVEL% == 0 (
        echo Process has already being kicked off for the files with date %YYMMDD%, skipping it...
        echo Download and backup compleated
        exit /b 0 
    ) else (
        echo Triggering next Jenkins Job:  
        curl -I http://<user>:<token>@remoteserver.domain.com:<port>/job/Hello_World/build?token=hello_world
        exit /b 0 )
)

For more information on this type in if /? into cmd.

Note: I also fixed I believe a typo in your code of findsrt instead of findstr

Nico Nekoru
  • 2,840
  • 2
  • 17
  • 38