0

I am writing a small BAT file where it will search for "FAIL" Keyword followed by PASS - if none is found then take it as an error:

echo
set "topLevel=%cd%"
If [%1]==[] exit /B 1
If [%2]==[] exit /B 1
If [%3]==[] exit /B 1
If [%4]==[] exit /B 1
findstr /? >NUL 2>&1 || exit /B 1
set "arg1=%1"
set "arg2=%2"
set "arg3=%3"
set "arg4=%4"

set /a errno=0
if not exist %arg3% exit /B 1
if not exist %arg2%\%arg1% exit /B 1
set "logfile=%arg1:.=_%"
copy /y/v %arg2%\%arg1% %arg3%\%arg4%.%logfile%.res || exit /B 1
findstr /I /C:"FAIL" /I /C:"UNKNOWN" %arg3%\%arg4%.%logfile%.res 
if %errorlevel% EQU 0 (
    set /a errno=2
) ELSE (
    REM MAKE SURE THAT THE SCRIPT DID NOT CRASH HENCE NEITHER PASS OR FAIL WILL BE LISTED
    findstr /I /C:"PASS" %arg3%\%arg4%.%logfile%.res  
    if %errorlevel% NEQ 0 (
    set /a errno=2
    )
)  
cd %topLevel%
exit /B %errno%

When I run with sample data I get below output:

..............................................

    C:\agent\_work\30\s1>copy /y/v C:\output\test.log C:\agent\_work\30\s1\tttt.test_log.res   || exit /B 1
            1 file(s) copied.

    C:\agent\_work\30\s1>findstr /I /C:"FAIL" /I /C:"UNKNOWN" C:\agent\_work\30\s1\tttt.SystemWalk_log.res

    C:\agent\_work\30\s1>if 1 EQU 0 (set /a errno=2 )  ELSE (
    REM MAKE SURE THAT THE SCRIPT DID NOT CRASH HENCE NEITHER PASS OR FAIL WILL BE LISTED
     findstr /I /C:"PASS" C:\agent\_work\30\s1\tttt.test_log.res
     if 1 NEQ 0 (set /a errno=2 )
    )
    PASSED
    PASSED
    PASSED
    PASSED
    PASSED

    C:\agent\_work\30\s1>cd C:\agent\_work\30\s1

    C:\agent\_work\30\s1>exit /B 2

    C:\agent\_work\30\s1>echo %ERRORLEVEL%
    2

Actually cause it has found "PASS" string and no "FAIL" ones - so the error level should be 0 - how can I fix the issue?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Programmer
  • 8,303
  • 23
  • 78
  • 162
  • Sorry I am using Win 10 – Programmer Jul 03 '19 at 08:52
  • Your posted output it is not a true reflection of that from running the script you posted. It is clear that `%1` was either `test.log` or `SystemWalk.log`, not both, I'll assume that the actual argument was the latter. Please take care in future when needlessly changing things. It is absloutely unreasonable to expect that some evil genius could identify your PC, yourself, your employer etc. by the name of a file in `C:\output` of your system. – Compo Jul 03 '19 at 10:44

1 Answers1

2
 if %errorlevel% NEQ 0 (

should be

 if errorlevel 1 (

Standard delayedexpansion issue - you need to invoke delayedexpansion [hundreds of SO articles about that - use the search feature] in order to display or use the run-time value of any variable that's changed within a parenthesised series of instructions (aka "code block").

Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.

IF ERRORLEVEL n is TRUE if errorlevel is n or greater than n. IF ERRORLEVEL 0 is therefore always true. IF NOT ERRORLEVEL 1 is a test for errorlevel=0. So is IF %ERRORLEVEL%==0, except that the former can be used within a block but the latter cannot.

Magoo
  • 77,302
  • 8
  • 62
  • 84