3

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.

4

create an array and loop through, provide different "goto" if necessary

@echo off
set langList=("language1()","language2","language3")

:Get_ISOlanguage
set "ISOlanguage="
set /p ISOlanguage=Enter subtitle language in ISO 639-2 standard abbreviation:
set ISOlanguage="%ISOlanguage%"

for %%x in %langList% do (
if ""%ISOlanguage%"" == ""%%x"" (echo LANGUAGE SUPPORTED
goto :CONTINUE))
echo LANGUAGE NOT SUPPORTED 
goto :GET_ISOlanguage

:CONTINUE
echo ayy lmao

pause

--------------------EDIT--------------------

@echo off
set langList1[1]="lang1()"
set langList1[2]="lang2"
set langList1[3]="lang3"

set langList2[1]="language1()"
set langList2[2]="language2"
set langList2[3]="language3"

set index=0

:Get_ISOlanguage
set "ISOlanguage="
set /p ISOlanguage=Enter subtitle language in ISO 639-2 standard abbreviation:
set ISOlanguage="%ISOlanguage%"

for /l %%x in (1,1,3) do (set /a index+=1
setlocal enableDelayedExpansion
if "%ISOlanguage%" == "!langList1[%%x]!" (set language=!langList2[%%x]!
goto :CONTINUE))
set index=0
echo LANGUAGE NOT SUPPORTED 
goto :GET_ISOlanguage

:CONTINUE
echo LANGUAGE SUPPORTED
echo YOU CHOSE %language:"=%
pause
  • 1
    not an issue, you can just double quote everything in your array, see code edit above, when you type in your preferred language you don't need to include the quotes. – vng21092 Feb 7 '16 at 21:06
  • Thanks a lot. Let's say, if the user enters eng, and eng is a part of the langlist how can I get a message saying You have entered english I have tried working out this for hours now. Please help me out if you can. – Arete Feb 8 '16 at 19:33
  • Sorry for the late response but this tickled my brain also, only way I worked through was recreating the array, I'm not a batch expert so I'm not too familiar with the way variables are set or called (all these percent signs and exclamations, weird syntax bro). But I'll place what I have above. – vng21092 Feb 9 '16 at 14:16
2

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

It is not clear from your question exactly what you want to do, but here is a small example you can use as a starting point. See the notes below for a suggestion on how you could improve it.

@echo off
:Get_ISOlanguage
set /p ISOlanguage=Enter subtitle language in ISO 639-2 standard || Set ISOlanguageabbreviation=NothingChosen
If "%ISOlanguage%"=="NothingChosen" goto :Get_ISOlanguage
If /i "%ISOlanguage%"=="UK" goto sub_uk
If /i "%ISOlanguage%"=="FR" goto sub_fr
rem add more options here
If /i ...
rem end of more options
rem if we get here it must be invalid input so try again
goto Get_ISOlanguage


:sub_uk
echo You chose UK
goto:eof

:sub_fr
echo You chose FR

Notes:

  • "%ISOlanguage%" contains the user's choice
  • The above code is based on an example in the source link.

  • To simplify the batch file you could have another file containing a list of valid ISO names (one per line)

  • Use findstr to check for valid input instead of multiple if statements

  • Something like (not tested):

    find /i "%ISOlanguage%" ISONames.txt
    If %ERRORLEVEL% EQU 0 echo "Valid input"
    

Source set


Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
0
@ECHO OFF
SETLOCAL
: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

:: First way

SET "validlanguages=eng,fr,ger"

FOR %%a IN (%validlanguages%) DO IF /i "%%a"=="%ISOlanguage%" GOTO valid1
ECHO way1 - invalid language "%ISOlanguage%"
GOTO way2

:valid1
ECHO way1 - Valid language "%ISOlanguage%"


:: Second way - languages in file 'q1036836.txt' - one to a line

:way2
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q1036836.txt"
ECHO %ISOlanguage%|FINDSTR /i /x /g:"%filename1%" >NUL
IF ERRORLEVEL 1 (
 ECHO way2 - invalid language "%ISOlanguage%"
) ELSE (
 ECHO way2 - Valid language "%ISOlanguage%"
)

:: Third way - languages in file 'q1036836.txt' - one to a line


SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q1036836.txt"
FINDSTR /i /x /L /c:"%ISOlanguage%" "%filename1%" >NUL
IF ERRORLEVEL 1 (
 ECHO way3 - invalid language "%ISOlanguage%"
) ELSE (
 ECHO way3 - Valid language "%ISOlanguage%"
)

:: Fourth way - languages/destination in file 'q1036836_2.txt' - one to a line


SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q1036836_2.txt"
FOR /f "usebackqtokens=1*delims=," %%a IN ("%filename1%"
 ) DO IF /i "%%a"=="%ISOlanguage%" SET "$goto=%%b"&GOTO valid4
ECHO way4 - invalid language "%ISOlanguage%"

GOTO :eof

:valid4
ECHO way4 - Valid language "%ISOlanguage%" - GOTO "%$goto%"

GOTO :EOF

You would need to change the settings of sourcedir and filename1 to suit your circumstances.

I used a file named q1036836.txt containing this for my testing.

eng
fr
ger

The first method simply iterates over each item in a list and on finding a match goes to the valid label. If the list is exhausted, try again.

The second method and third method exploit findstr setting errorlevel=0 if there is a match found and non-zero otherwise.

The third method is simply another way of using findstr (and would be my preference.)

The fourth method allows different destinations to be specified. On valid entry, a line goto %$goto% would jump to the appropriate destination label.

I used a file named q1036836_2.txt containing this for my testing.

eng,dest_for_eng
fr,destination_for_fr
ger,vere_to_go_for_ger

With any of these methods, I've used the complete syntax. No doubt it could be abbreviated if required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.