批处理文件:将选择从动态菜单导出到变量?

时间:2016-01-09 16:29:43

标签: variables batch-file if-statement menu set

好的,这里......

我正在构建一个批处理文件,其中包含根据多个ini文件中的设置动态设置的菜单,我使用了上一个问题here中向我建议的脚本。

现在,我需要添加用户输入,将选定的菜单选项导出到另一个变量,在普通菜单中,没有动态设置它很容易,我做了很多次但是我可以& #39;在这种情况下弄清楚如何做到这一点。

这是我目前的剧本:

@echo off
setlocal EnableDelayedExpansion


:LangMenu
for /L %%D in (1,1,20) do (
   if exist Common\Settings\Data%%D.ini for /F "eol=# tokens=1,2 delims==" %%a in (Common\Settings\Data%%D.ini) do (

      set line=%%a
      if "!line:~2,5!" neq "clude" (
         REM Define "normal" variables, i.e. Compressor, Method, etc.
         set %%a=%%b
      ) else if "!line:~7!" neq "" (

         REM Define the base array elements, i.e. D1IncludeAR=%%b, D1ExcludeAR=%%b, ...
         set D%%D%%a=%%b

         REM Set Show?? array elements with value equal 1, i.e. ShowAR=1, ...
         REM when anyone of DiInclude?? or DiExclude?? corresponding elements was given
         if defined D%%D%%a set Show!line:~7!=1

      )
   )
)


:LangContinue

REM Define a list of language abbreviations, i.e. "langs=AR CZ DE ..."
REM and the corresponding language names array, i.e. lang[AR]=Arabic, ...
REM At same time, calculate original OptNum

for %%a in ("AR=Arabic" "CZ=Czech" "DE=German" "EN=English" "ES=Spanish" "ESMX=Spanish (Mexico)"
            "FR=French" "HU=Hungarian" "IT=Italian" "JP=Japanese" "KR=Korean" "PL=Polish"
            "PR=Portuguese" "PRBR=Portuguese (Brazil)" "RU=Russian" "ZH=Chinese") do (
   for /F "tokens=1,2 delims==" %%b in (%%a) do (
      set "langs=!langs! %%b"
      set "lang[%%b]=%%c"
      set /A "OptNum+=Show%%b"
   )
)

:LangSelect
cls
REM Show the language menu
set #=0
for %%a in (%langs%) do (
   if defined Show%%a (
      set /a #+=1
      echo [!#!] !lang[%%a]!
   )
)

set /p SelectLang=Please Choose The Language You Want: 
if not "%SelectLang%"=="" Goto LangSet
if "%SelectLang%"=="" goto LangError


:LangError
cls
Echo ERROR: No Language Selected!!
pause
goto LangSelect

:LangSet
if "%SelectLang%"=="1" set LangIs=1
echo Selected Language = %LangIs%

请注意:我只是暂时把1放在套装中,所以我不会得到回声消息,它根本不是我所需要的,只是占位符。

我需要的是将LangIs变量设置为拾取的选项......

一个例子: 菜单输出为:

[1] Arabic
[2] German
[3] English
[4] Spanish
[5] Italian
Please choose the language you want:

上例中的变量语言可以是任何语言,甚至可以列出最多16种语言,这些数字是动态设置的,所以如果可用的语言中有不同的5种语言,它仍然是1-5。

我需要一种方法,用户输入的编号选项将写出一个带有显示的语言名称的变量,因此,例如,如果用户键入5,我需要一个变量,它将具有&#34 ;阿拉伯"或" AR"在它。

所以,例如,如果我使用echo命令来显示选项,如果选择数字1,它应输出类似的内容(在上面的菜单示例中):

Selected Language = AR

Selected Language = Arabic

或者在菜单中为数字1指定了任何语言。

此变量必须稍后在我的脚本中检查最多20次,以检查选择的语言,以便根据所选语言设置另一个变量。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

:LangSelect
cls
REM Show the language menu
set #=0
for %%a in (%langs%) do (
   if defined Show%%a (
      set /A #+=1
      echo [!#!] !lang[%%a]!
      set "option[!#!]=%%a"
   )
)

set /P "SelectLang=Please Choose The Language You Want: "
if defined option[%SelectLang%] Goto LangSet

:LangError
cls
Echo ERROR: Invalid Language Selected!!
pause
goto LangSelect

:LangSet
set "LangIs=!option[%SelectLang%]!"
echo Selected Language = %LangIs% (!lang[%LangIs%]!)