Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch - check if variable is not empty and perform action

I have the following batch code:

set checkerr=
for /f "delims=" %%a in ('findstr "Error" %LOG%') do @set checkerr=%%a


echo %checkerr%


if "%checkerr%" NEQ [] GOTO sendmail

But it is not working, I want it to send a mail only if the text string "Error" is found in the log file

Note: Errors in the log file might look like this:

20131117185439:       Error: Field not found - <blablabla>
20131117185445:       General Script Error
20131117185445:       Execution Failed
20131117185445:      Execution finished.

So there are spaces and other chars, etc

Regards

like image 972
anarchist Avatar asked Nov 24 '25 13:11

anarchist


1 Answers

The && operator executes the following command when errorlevel zero is generated by the preceeding command, so this should be equivalent and will branch to the :sendmail label when 'Error' is found in the log file.

findstr "Error" "%LOG%" >nul && goto :sendmail

The companion operator to that is || and this executes the following command on an errorlevel that is not zero.

To check if a variable is empty you can use this:

if not defined variablename goto :sendmail
like image 144
foxidrive Avatar answered Nov 26 '25 22:11

foxidrive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!