This question already has an answer here:
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?
should be
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 aFOR ... DO (block)
.Hence,
IF (something) else (somethingelse)
will be executed using the values of%variables%
at the time theIF
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 ofvar
or 2) to call a subroutine to perform further processing using the changed values.IF ERRORLEVEL n
is TRUE iferrorlevel
is n or greater than n.IF ERRORLEVEL 0
is therefore always true.IF NOT ERRORLEVEL 1
is a test for errorlevel=0. So isIF %ERRORLEVEL%==0
, except that the former can be used within a block but the latter cannot.