2017-04-26 144 views
0

我是批量腳本編程新手,嘗試執行以下if語句,並且命令提示符返回有關語法的錯誤。我在互聯網上搜索過,似乎無法得到直接的答案,如何在批處理文件中執行if else語句。什麼是格式聲明的正確方法?任何幫助將不勝感激。if else批處理文件中的語句格式

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) 
        ) 
+0

它也應該是'回聲sent'不'sent'在'發送>%CD%\ EmailSent.txt%' – Compo

回答

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

空間顯著。 ) else (必須在一條線上。

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

是的,我知道。我如何做多行if else語句? –

1

這可能不是最好的代碼,但這或多或少是如何。

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

您可以通過運行該腳本來看到不同的結果之前做set this=thisset this=not_thisset this=something_else測試。

0

我會減少冗餘和使用更好的縮進:

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." 
    ) 
) 

目前還不清楚我的sent>%cd%\EmailSent.txt應該做的事情。

+0

OP問如何做多個'else if'他具體說'如果其他' –

+0

@GerhardBarnard從字面上他問'格式化這個語句的正確方法是什麼?' – LotPings

+0

標題'if else語句格式在批處理文件中內容'我在互聯網上搜索過,似乎無法得到一個直接的答案,如何做一個批處理文件中的if else語句和Magoo的回答中的評論'是的,我知道這一點。我如何做多行if else語句?' –

0

我已經添加了這個答案,因爲你和LotPings代碼塊都包含不必要的代碼。 (電子郵件在每種情況下都發送,這意味着您只能發送和寫入文件一次)

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" 
)