0

OK, here goes...

I'm building a batch file with a menu that is set dynamically according to settings in multiple ini files, I used a script suggested to me in a previous question here.

Now, I need to add user input that export the selected menu option to another variable, in normal menu, that is not set dynamically it's easy enough, I've done it many times but I can't figure out how to do so in this instance.

Here's my current script:

@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%

Please note: I just put 1 in the set LangIs for the time being so I won't get echo is off message, it's not at all what I need, just a placeholder.

What I need is to set LangIs variable to the picked option...

An example: Menu output is this:

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

The variable language in the above example can be any languages, even up to 16 languages can be listed, the numbers are set dynamically so if available languages where different 5 languages it was still 1-5.

I need a way that the numbered choice entered by the user will write a variable with the Language name shown by it, so, for example, if the user type 5, I need a variable, that will have either "Arabic" or "AR" in it.

so, for example, if I use the echo command to show the choice it should output something like this if the number 1 is selected (in above menu example):

Selected Language = AR

or

Selected Language = Arabic

Or whatever language is assigned to the number 1 in the menu.

This variable, will have to be checked later on in my script up to 20 different times to check what language is selected in order to set another variable according to the selected language.

Any suggestions will be much appreciated.

Community
  • 1
  • 1
VollachR
  • 35
  • 8

1 Answers1

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%]!)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I'd consider using `choice` instead of `set /P`. – JosefZ Jan 09 '16 at 18:38
  • @JosefZ : I thought of that, decided against it since the menu can have up to 16 options to choose from and I don't think choice support 2 characters choices (unless I'm wrong, in that case, please correct me) – VollachR Jan 10 '16 at 09:15
  • Oh, yeah, that answer worked well, at least as far as I tested so far, just made a little change `echo Selected Language = %LangIs%` instead of `echo Selected Language = %LangIs% (!lang[%LangIs%]!)`, for one reason, it results in a shorter value being echoed for the LangIs variable, just the short language code (AR, CZ, PRBR, etc.), I don't need the AR (Arabic), EN (English) and so on, I need the exact LangIs value echoed, since the echo is just a placeholder to check that it works, there will be a check for the AR, CZ, etc, values that set another value much further in the script. – VollachR Jan 10 '16 at 09:31
  • OK, there's something missing from the script after all, I need to skip the menu if all Include/Exclude variables for the languages are not defined, but only in that case, everything I tried causes the menu to skip even if `Show%%a` is defined, I need to skip the menu and go to label `:SkipMenu` only if `Show%%a` is not defined at all, for any language. – VollachR Jan 10 '16 at 11:29
  • **1.** Your last but one _large_ comment makes no sense. We are not interested in details about how you use _your_ code. Why you posted it? Please, be respectful of our time; I already said you to be as concise as possible. **2.** To skip the menu, after `if defined D%%D%%a set Show!line:~7!=1` add `& set ShowAny=1`. Then, `if not defined ShowAny goto SkipMenu` – Aacini Jan 10 '16 at 18:10
  • OK... **1.** Yeah, ok, I may have got carried away with the details on that comment, it happens to me sometimes, I'll try and avoid it in the future. **2.** Thanks, I found a way to get the skip function working myself though, and without adding more variables, I was tired when I tried yesterday, the solution came to me when I was in bed. Anyway, basically, I still have the `OptNum` variable which counts the `Show%%a` variables and sum them up, if there are no `Show%%a` it is set to 0, so `if "%OptNum%"=="0" Goto SkipMenu` worked. – VollachR Jan 11 '16 at 08:44