Arete
Arete

Reputation: 1004

Batch command how to make a list for set /p

I am trying to make a batch script that asks for user input. The user input should be limited to certain words. If the user enters any other words than e.g. (eng, ger, fra) the user will get an echo saying something like "Please enter a language".

I also want to store the user input as a variable for further use in the batch file.

What I have so far is:

:Get_ISOlanguage
set "ISOlanguage="
set /p ISOlanguage=Enter subtitle language in ISO 639-2 standard abbreviation:
if not defined ISOlanguage echo You must enter a value. Try again.&goto Get_ISOlanguage

Now how can i list all the accepted words and make these words the only accepted words for the input?

The list of word is very long and I probably don't need to include it here.

Upvotes: 1

Views: 587

Answers (1)

Stephan
Stephan

Reputation: 56189

get the list into a file and compare your input with that file:

:Get_ISOlanguage
set "ISOlanguage="
set /p "ISOlanguage=Enter subtitle language in ISO 639-2 standard abbreviation: "
find /i "%ISOlanguage%" ISO6392.txt >nul || (echo wrong input & goto :Get_ISOlanguage)

To create the file, I copied the list from here to a temp file and extracted the codes with for /f "delims=- " %i in (ISO-list.txt) do @echo %i)>ISO6392.txt (one-time-task)

Notes:

I used set /p "var=prompt: " syntax for clear definition of what is displayed (note the space, which is part of the prompt)

>nul redirects command output to NUL (nirvana). You probably don't want to see it on the screen.

|| works as "if previous command (find) failed, then" (opposite would be &&)

Upvotes: 3

Related Questions