Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Why does this IF statement not work (DOS) BATCH

Hi, I am trying to call a function when a variable has been set, why wont it call the function when MAILERROR equals YES?

@ECHO OFF
SET MAILERROR=NO
:START
GOTO ERRORCOPY
IF MAILERROR == NO GOTO SENDMAILERROR
QUIT

:ERRORCOPY
ECHO.
ECHO A critical error occured whilst tranfering orginal files - The program will now terminate.
SET MAILERROR=YES
GOTO START

:SENDMAILERROR
ECHO SENDING MAIL
PAUSE
EXIT

0
zoomzoomdevelopers
Asked:
zoomzoomdevelopers
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
If that's your exact code, it's because you're not calling the variable.  Variables, once set, must be referenced with %variableName%.  In addition, you should always enclose the variable and comparison value in quotes (or brackets or SOMETHING - this way if the variable ISN'T set for some reason, you don't crash the batch file.) - Try this:

@ECHO OFF
SET MAILERROR=NO
:START
GOTO ERRORCOPY
IF "%MAILERROR%" == "NO" GOTO SENDMAILERROR
QUIT

:ERRORCOPY
ECHO.
ECHO A critical error occured whilst tranfering orginal files - The
program will now terminate.
SET MAILERROR=YES
GOTO START
 
:SENDMAILERROR
ECHO SENDING MAIL
PAUSE
EXIT
0
 
nikkilockeCommented:
Hi zoomzoomdevelopers,

I'm not sure I understand the question, because following the batch file through makes it obvious what is happening.

Let's do that...

SET MAILERROR=NO
GOTO ERRORCOPY
[jumps to...]
:ERRORCOPY
ECHO.
ECHO.
ECHO A critical error occured whilst tranfering orginal files - The program will now terminate.
SET MAILERROR=YES
GOTO START
[jumps to...]
:START
GOTO ERRORCOPY
[continue to loop round for ever!]

Hope this helps,

Nikki
0
 
zoomzoomdevelopersAuthor Commented:

Sweeter than a sweet thing!

Thanks for your help leew.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Glad to be of help.

If you feel the question has been answered, please click the "Accept" button next to the comment I made (same line as my name) to close the question.

(In addition, you might want to use "/i" in the if statement AND NOT TYPE EVERYTHING IN CAPS - Your code can be easier to read that way.

For example:
IF /i "%MAILERROR%" == "NO" GOTO SENDMAILERROR

The /i tells it to compare case insensitive - meaning "no" == "NO" is TRUE.
0
 
PaulCaswellCommented:
FYI:

This style:

IF /i "%MAILERROR%" == "NO" GOTO SENDMAILERROR

will not be compatible with older DOS versions. The most portable form is:

IF (%MAILERROR%)==(NO) GOTO SENDMAILERROR

use of quotes can have strange consequences and you certainly dont have /i in earlier DOS systems.

You CAN use

IF %MAILERROR%==NO GOTO SENDMAILERROR

if you want but if for some reason you havent set MAILERROR the expression evaluates to:

IF ==NO GOTO SENDMAILERROR

which would give a syntax error.

Adding delimiters such as ( and ) avoids this issue by making the above evaluate to:

IF ()==(NO) GOTO SENDMAILERROR

which at least does not fail.


Paul
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now