2011-09-01 90 views

回答

12

if "%var%"=="one" goto foo 
if "%var%"=="two" goto foo 
if "%var%"=="three" goto foo 
goto afterfoo 
:foo 
echo The number is between one and three (technically incorrect, since it includes the end points and thus is not between). 
:afterfoo 

如果你需要一個更結構化的方法:

if "%var%"=="one" set OneToThree=1 
if "%var%"=="two" set OneToThree=1 
if "%var%"=="three" set OneToThree=1 
if defined OneToThree (
    echo Foo 
) else (
    rem something 
) 
+0

想法=(。我一直試圖用它欺騙它一段時間,但沒有運氣大聲笑 – Mechaflash

+2

你也可以使用這種方法:對於%% a在(一二三)如果「%var% 「==」%% a「goto foo – Aacini

0

在遊戲後期了一下,但仍然假定如果這可能會幫助任何人絆倒這個問題。我這樣做的方法是使用回聲的組合通過管道輸送到FINDSTR,這種方式:

(echo ":one: :two: :three:" | findstr /i ":%var%:" 1>nul 2>nul) && (
    echo The number is between zero and four 
) 

由於FINDSTR是一個外部命令,我建議不使用此一環,它可以經過反覆的1000的內部。如果不是這種情況,這應該解決你試圖做的事情,而不是使用多個ifs。另外,在選擇「:」時沒有什麼特別的地方,只需使用一個不太可能成爲var變量值的一部分的分隔符。

感謝其他人指出另一個似乎有類似問題的鏈接,我也會在這裏發佈這個回覆,以防萬一有人在這個問題上磕磕絆絆,而且還沒有到達這裏。