2014-07-26 92 views
1

如果提示您如何允許用戶在批處理腳本中鍵入多個數字?批處理腳本多選

如果它向用戶提示5個選項。並且用戶想要選擇1,2,3但不想要4,5,如何在批處理腳本中執行此操作?

我refered到Allowing multiple choice selections for users using batch fileMultiple choices menu on batch file?

有計算器中的相關問題,但沒有答案,因爲答案是不正確的地步。

任何幫助將不勝感激。

+0

什麼發人深省的問題(什麼你真的想完成)? –

+0

因爲我也想知道答案。我在網上搜索了答案,但無濟於事。另外我也是批處理腳本的新手。 – Steven

+1

爲什麼其他答案不能回答你的問題? –

回答

1

這個答案也提到了這個問題:How to integrate batch script multiple selections into JAVA GUI?

你可能想在頂部加入一行刪除family.txt如果你想要一個新的文件,每次。

@echo off 
echo. 
echo Selection time! 
echo. 
echo 1. My father is Joe 
echo 2. My mother is Audrey 
echo 3. My brother is Jerry 
echo 4. My elder sister is June 
echo 5. My youngest sister is Awy 
echo 6. Include All 
echo. 

:getOptions 
set "choices=" 
set /p "choices=Type your choices without spacing (e.g. 1,2,3): " 

if not defined choices ( 
    echo Please enter a valid option 
    goto getOptions 
    ) 

for %%a in (%choices%) do if %%a EQU 6 set choices=1,2,3,4,5 
for %%i in (%choices%) do call :option-%%i 

echo. 
echo Done 
pause 
exit 

:option-1 
>> Family.txt echo My father is Joe 
exit /B 

:option-2 
>> Family.txt echo My mother is Audrey 
exit /B 

:option-3 
>> Family.txt echo My brother is Jerry 
exit /B 

:option-4 
>> Family.txt echo My elder sister is June 
exit /B 

:option-5 
>> Family.txt echo My youngest sister is Awy 
exit /B 
+0

但是,如果我輸入** 1,2 ** – Steven

+0

代替%% i in(%options%)do call:option - %% i',我仍然會得到'2在這個時候出乎意料'錯誤,它是隻有插入1個數字才能運行。但是如果插入** 1,2 **,則會出現錯誤 – Steven

+0

不,沒有錯誤。調用你的批處理文件'Mychoices.bat'並嘗試它。它在這裏很好用'1,2,3'或'4,5,6'或'6' – foxidrive

1

像這樣:

@echo off 

set /p "$choix=Enter you(s) Choice (1,2,...): " 
for %%a in (%$choix%) do call:choix%%a 
exit/b 


:choix1 
echo Choice 1 
exit/b 

:choix2 
echo Choice 2 
exit/b 

:choix3 
echo Choice 3 
exit/b 
+0

是的 - 逗號在自然循環中被自動視爲空格,因此您不必將其轉換爲空格。 – foxidrive

+0

真的,我總是忘記它,謝謝@ foxidrive。更正 – SachaDee

+0

我甚至無法運行編碼。 (%$ options:,=%)%% a call:選項 - %% a' – Steven