Batch programming setting ERRORLEVEL from failed FIND command
Asked Answered
S

3

10

I have found the need to report an error level to a program that calls batch scripts.

The script will create a CSV log file that I would like to check in order to display whether the script was run successfully.

What I would like to say is something like

IF NOT FIND "[ERR" "test.csv"

or to say, that if the string "[ERR" is not in the output ERRORLEVEL = 0 ELSE ERRORLEVEL = 1

Hope this makes sense. I'm sure it is simple but setting error levels seems to be a royal pain in the a$se!

Shererd answered 23/10, 2012 at 13:48 Comment(0)
R
12

FIND "[ERR" "text.csv" sets ERRORLEVEL to 0 if at least one [ERR is found, and 1 if no [ERR is found. You need a way to invert the logic.

If all you want to do is immediately return 0 if no [ERR found, or 1 if at least one [ERR found, then I would use the following:

find "[ERR" "test.csv" >nul && exit /b 1 || exit /b 0

If you want to capture the result in a variable to be returned later, then:

find "[ERR" "test.csv" >nul && set "err=1" || set "err=0"

or

find "[ERR" "test.csv" >nul
set /a "err=!%errorlevel%"

When you are ready to return the result

exit /b %err%

Below is my original answer that was accepted, yet was embarrassingly wrong :-(
Thanks Corey for pointing out the logic error. As he says in his comment, the faulty code below will only report an error if all of the lines have the [ERR text.

FIND will set the ERRORLEVEL to 0 if found, 1 if not found. To reverse the logic, simply use the /V option.

find /v "[ERR" "test.csv" >nul

If you have a variable set that indicates the errorlevel, then you can use EXIT /B to set the ERRORLEVEL

set err=1
exit /b %err%
Raye answered 23/10, 2012 at 15:1 Comment(6)
Hi dbenham,Thank you for your reply but unfortunately just adding /V seems to result in the Errorlevel always being set to 0. Also I've tried using this batch file to test the second response.FIND "[ERR" "test.csv" SET MYERROR = 1 EXIT /B %MYERROR% it should always return 1 when I echo %errorlevel% but it will only do this if the [ERR is not in the test.csv file! Am I doing something wrong with the syntax? All 3 statements are on separate lines but I don't seem to be able to start a new line to represent this! thank you in advance of your reply!Shererd
dbenham! After running through this again, I found that I had made what is DOS schoolboy error! I put set err = 1 instead of err=1. For anyone else reading this, what this means is that, when you type SET ERR =1 the variable name is 'ERR ' not 'ERR'. Thank you!Shererd
Specifying /V will only result in a failure (ERRORLEVEL == 1) if all lines in the file contain the specified value. It reverses the test from contains x to does not contain x, with ERRORLEVEL being 0 if any lines are found that match the test.Berl
@Berl - OMG, you are correct, my logic does not give the desired result! I will edit my answer. I am amazed no one ever pointed out the error in 5+ years, especially the OP.Raye
Sorry about zombie posting... I was looking for this and found it was backwards.Berl
@Berl - Hell, don't apologize. It is important that info like this gets corrected.Raye
C
4

Note that the IF command has the logic that it returns true if ERRORLEVEL is greater than or equal to the following number! So

IF ERRORLEVEL 0 (...

returns true for all FIND commands. What you need if you only want to test if a phrase is in a file:

find "string" "test.txt" > NUL & IF NOT ERRORLEVEL 1 ECHO String was found

Took me ages to work that out!

Coattail answered 4/8, 2017 at 9:53 Comment(1)
&& is a kind of shortcut for IF NOT ERRORLEVEL 1, so you could change your line to: find "string" "test.txt" > NUL && echo String was found || echo String was not foundManner
D
3

This is the logic I've used in a BAT file previously.

find "string" "test.txt" > NUL & IF ERRORLEVEL 1 (
    ECHO String was NOT found
) ELSE (
    ECHO String was found
)

You could put whatever commands you wish inside the IF/ELSE blocks.

Derrickderriey answered 12/11, 2015 at 0:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.