anarchist
anarchist

Reputation: 393

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

Upvotes: 3

Views: 16828

Answers (1)

foxidrive
foxidrive

Reputation: 41234

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

Upvotes: 4

Related Questions