1

The batch file with the following code branches depending on %ERRORLEVEL% condition. set result=PASSED is executed if string OK is found in output. The batch file should also search for string ERROR in addition to OK string in output and should execute set result=FAILED if ERROR is found in output.

How can I do this?

rem ...
rem ...
call myCommand.cmd | FIND "OK" > NUL

if %errorlevel% == 0 (
    set result=PASSED
    set /A passedCounter=passedCounter+1
) else (
    set result=FAILED
    set /A failedCounter=failedCounter+1
)
rem ...
rem ...
Mofi
  • 46,139
  • 17
  • 80
  • 143
Jamil Rahman
  • 345
  • 1
  • 6
  • 18
  • Is it feasible to run the first line again but search for ERROR this time? Otherwise, you'll have to redirect the output of `call mycommand.cmd` to a file and do searches on that. Either way, you need a separate ` find` command. – SomethingDark Jul 05 '19 at 02:41
  • It will repeat the same command (myCommand.cmd) consecutively, which is not desirable. – Jamil Rahman Jul 05 '19 at 22:13
  • Something doesn't look right with your question. If you really need to check for both OK and ERROR, then you need to consider if both are found, or if neither is found. If it is always exactly one or the other, but never both, then your existing logic is likely good. – dbenham Jul 08 '19 at 14:10

1 Answers1

1

This batch file code could be used for this task.

@echo off
call myCommand.cmd | %SystemRoot%\System32\findstr.exe /I /L "OK ERROR" >"%TEMP%\%~n0.tmp" || goto Failed
%SystemRoot%\System32\findstr.exe /I /L "ERROR" "%TEMP%\%~n0.tmp" >nul && goto Failed

set "result=PASSED"
set /A passedCounter+=1
goto Done

:Failed
set "result=FAILED"
set /A failedCounter+=1

:Done
del "%TEMP%\%~n0.tmp" 2>nul
echo Result is: %result%

FINDSTR searches in standard output of batch file myCommand.cmd

  • case-insensitive because of option I
  • and literally because of option /L
  • for string OK OR string ERROR.

The space character inside double quoted search string is interpreted in this case by FINDSTR as OR expression.

The exit code of FINDSTR is 1 if neither OK nor ERROR could be found which should be interpreted as failed operation and so processing of batch file is continued on command line below the line with label Failed.

Otherwise the one or more lines containing OR OR ERROR output by FINDSTR are written into a temporary file in directory for temporary files with name of batch file as file name and file extension .tmp.

This temporary file with output of first executed FINDSTR is searched once again with FINDSTR this time searching case-insensitive and literally only for the string ERROR and suppressing the output by redirecting it to device NUL.

FINDSTR exits with 0 if there is a line with ERROR resulting in processing of batch file is continued on command line below the line with label Failed.

Otherwise the output of batch file myCommand.cmd contains OK, but not ERROR and so the task was accomplished successfully.

Finally the temporary file is deleted on existing at all with suppressing the error message on not existing by redirecting the error message to device NUL and the result is printed into console window.

See also:

Another solution not using a temporary file:

@echo off
set "result="
for /F delims^=^ eol^= %%I in ('myCommand.cmd ^| %SystemRoot%\System32\findstr.exe /I /L "OK ERROR"') do (
    echo %%I | %SystemRoot%\System32\findstr.exe /I /L "ERROR" >nul && goto Failed
    set "result=PASSED"
)
if defined result set /A "passedCounter+=1" & goto Done

:Failed
set "result=FAILED"
set /A failedCounter+=1

:Done
echo Result is: %result%

FOR with option /F starts a new command process in background with %ComSpec% /c and the command line as specified in the round brackets between the two ' which means with Windows directory on drive C: the execution of the command line:

C:\Windows\System32\cmd.exe /c myCommand.cmd | %SystemRoot%\System32\findstr.exe /I /L "OK ERROR"

The redirection operator | must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded command line with using a separate command process started in background.

FOR captures everything output by FINDSTR to handle STDOUT of background command process and processes the captured text line by line after started cmd.exe terminated itself.

FOR would split up each non-empty line into substrings using normal space and horizontal tab as string delimiters and would assign just first space/tab separated substring to specified loop variable I if not starting with default end of line character ;. This line splitting behavior with ignoring entire line on starting with a semicolon without or with leading spaces/tabs is not wanted here. For that reason delims= eol= is used to define an empty list of delimiters to disable line splitting behavior and define no end of line character. It is necessary to specify these two options without using double quotes which means the equal signs and the space character must be escaped with ^ to be interpreted as literal characters and not as argument separators.

Each line output by FINDSTR in separate command process is searched for string ERROR and if indeed found the loop execution is exited with a jump to command line below line with label Failed. Otherwise this line must contain OK and so the environment variable result is defined now with value PASSED. But there could be one more line with ERROR and so the loop is not exited in this case.

If the environment variable result is defined after execution of FOR there was a line output by batch file myCommand.cmd with OK and none with ERROR which means the task was accomplished successfully.

BTW: It would be much easier if the batch file myCommand.cmd would exit with 0 on success and a value greater 0 on any error. This could be very easily evaluated by the calling batch file.

See also:

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • del /?
  • echo /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • set /?
Mofi
  • 46,139
  • 17
  • 80
  • 143