Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch programming setting ERRORLEVEL from failed FIND command

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!

like image 390
Freddie Sizer Avatar asked Oct 23 '12 13:10

Freddie Sizer


1 Answers

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!

like image 122
Dave Avatar answered Sep 28 '22 06:09

Dave