John Roland
John Roland

Reputation: 63

if else statement format in batch file

I am new to batch scripting and am trying to execute the following if statement and the command prompt returns an error concerning the syntax. I have searched around on the internet and cannot seem to get a straight answer on how to do an if else statement in a batch file. What is the proper way to format this statement? Any help would be greatly appreciated.

if !cnt! geq 20 (
                    if !size! lss 5000000(sent>%cd%\EmailSent.txt
                        mailsend1.19.exe -to !email! -from [email protected] -ssl -smtp smtp.gmail.com -port 465 -sub "test" -M "The capture quality is not sufficient.  Please retake capture." -auth-plain -user "[email protected]" -pass) else(sent>%cd%\EmailSent.txt
                        mailsend1.19.exe -to !email! -from [email protected] -ssl -smtp smtp.gmail.com -port 465 -sub "test" -M "The capture quality is ok. Patient good to go." -auth-plain -user "[email protected]" -pass )
                    )

Upvotes: 0

Views: 2377

Answers (4)

Compo
Compo

Reputation: 38642

I have added this answer only because your and LotPings code blocks both contain unnecessary code. (The email is being sent in each scenario meaning that you can send and write to file only once).

If !cnt! GEq 20 (
    If !size! Lss 5000000 (
        Set "_M=The capture quality is not sufficient.  Please retake capture."
    ) else (
        Set "_M=The capture quality is ok.  Patient good to go."
    )
    MailSend1.19.exe -to !email! -from [email protected] -ssl -smtp smtp.gmail.com -port 465 -sub "test" -M "!_M!" -auth-plain -user "[email protected]" -pass
    Echo sent>"%cd%\EmailSent.txt"
)

Upvotes: 0

user6811411
user6811411

Reputation:

I'd reduce redundancy and use a better indenting:

Set MailArgs=-to !email! -from [email protected] -ssl -smtp smtp.gmail.com -port 465 -sub "test" -auth-plain -user "[email protected]" -pass 
if !cnt! geq 20 (
    if !size! lss 5000000 (
        sent>%cd%\EmailSent.txt
        mailsend1.19.exe %MailArgs% -M "The capture quality is not sufficient. Please retake capture."
    ) else ( 
        sent>%cd%\EmailSent.txt
        mailsend1.19.exe %MailArgs% -M "The capture quality is ok. Patient good to go."
    )
)

It's unclear to me what the sent>%cd%\EmailSent.txt should do.

Upvotes: 0

user7818749
user7818749

Reputation:

This is probably not the best code, but this is more or less how.

@echo off
IF %this%==not_this (
     echo no match
) ELSE IF %this%==this (
     echo matches
) ELSE (
     echo Did not find match
)

You can test it by doing set this=this or set this=not_this or set this=something_else before running the script to see different results.

Upvotes: 1

Magoo
Magoo

Reputation: 80033

if "string1" operator "string2" (dothis) else (dothat)

spaces significant. ) else ( must be on a single line.

if "string1" operator "string2" (
 dothis
 then dothis
 after that dothis
) else (
 dothat
 then dothat
 after that dosomethingelse
)

Upvotes: 0

Related Questions