0

So I've recently created a batch file with menu and let users input numbers between 0-17, but I want to make the batch file to goto startProg if the users input something else (not number 0-17) or input empty value. In my current batch file, it will pass empty value and other inputs to the next section (the :changeLang section).

My batch file:

::=================================================================
:startProg
@echo off
title The Sims 4 Language Changer
set lang=en_US
cls
::=================================================================
:menuList
echo This program will help you to change The Sims 4 language
echo Please run this program with Administrative Privileges!
echo Anyway, what's your choice, captain?
echo.
echo 0. None (Clear Registry)
echo 1. American English
echo 2. Czech (Czechia)
echo 3. Danish (Denmark)
echo 4. German (Germany)
echo 5. European Spanish
echo 6. Finnish (Finland)
echo 7. French (France)
echo 8. Italian (Italy)
echo 9. Japanese (Japan)
echo 10. Korean (South Korea)
echo 11. Dutch (Netherlands)
echo 12. Norwegian (Norway)
echo 13. Polish (Poland)
echo 14. Portuguese (Brazil)
echo 15. Russian (Russia)
echo 16. Swedish (Sweden)
echo 17. Chinese (Taiwan)
echo.
set choice=
set /p choice=Please type the number correctly:
if "%choice%"=="0" goto clearLang
if "%choice%"=="1" set lang=en_US
if "%choice%"=="2" set lang=cs_CZ
if "%choice%"=="3" set lang=da_DK
if "%choice%"=="4" set lang=de_DE
if "%choice%"=="5" set lang=es_ES
if "%choice%"=="6" set lang=fi_FI
if "%choice%"=="7" set lang=fr_FR
if "%choice%"=="8" set lang=it_IT
if "%choice%"=="9" set lang=ja_JP
if "%choice%"=="10" set lang=ko_KR
if "%choice%"=="11" set lang=nl_NL
if "%choice%"=="12" set lang=no_NO
if "%choice%"=="13" set lang=pl_PL
if "%choice%"=="14" set lang=pt_BR
if "%choice%"=="15" set lang=ru_RU
if "%choice%"=="16" set lang=sv_SE
if "%choice%"=="17" set lang=zh_TW
cls
::=================================================================
:changeLang
> "%temp%\language.reg" echo REGEDIT4
>>"%temp%\language.reg" echo.
>>"%temp%\language.reg" echo [HKEY_LOCAL_MACHINE\SOFTWARE\Maxis\The Sims 4]
>>"%temp%\language.reg" echo "Locale"="%lang%"
>>"%temp%\language.reg" echo.
>>"%temp%\language.reg" echo [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Maxis\The Sims 4]
>>"%temp%\language.reg" echo "Locale"="%lang%"
>>"%temp%\language.reg" echo.
regedit /S "%temp%\language.reg" >nul
"%windir%\regedit.exe" /S "%temp%\language.reg" >nul
del /F /Q "%temp%\language.reg" >nul
cls
::=================================================================
:finalStep
echo The Sims 4 language changed to %lang%!
pause
exit
::=================================================================
:clearLang
reg delete "HKLM\SOFTWARE\Maxis\The Sims 4" /F
reg delete "HKLM\SOFTWARE\Wow6432Node\Maxis\The Sims 4" /F
cls
echo The Sims 4 registry data cleared!
pause
exit

The problem is that I don't want to change:

if "%choice%"=="XX" set lang=XXXXX

to:

if "%choice%"=="XX" goto XXXXX

Because it will make my batch file bigger by creating many goto XXX sections.

I also do not want to create multiple if not command because it's not efficient.

Andhika
  • 5
  • 6
  • 1
    You could just write `set lang=` before all the `if` queries, then use `if not defined lang goto XXXXX`, for example... But do you really need that as you seem to have a default language defined anyway (`set lang=en_US` at the top)? – aschipfl Aug 12 '18 at 19:49
  • @aschipfl Yeah, I was thinking to set en_US language to default if no input were choosen, but then I changed my mind and decided to make it 'goto startProg' instead of continuing en_US as the default input – Andhika Aug 13 '18 at 10:37

2 Answers2

2

This is the way I would do it using an array:

@echo off
setlocal EnableDelayedExpansion

title The Sims 4 Language Changer
cls

::=================================================================
:menuList
echo This program will help you to change The Sims 4 language
echo Please run this program with Administrative Privileges!
echo Anyway, what's your choice, captain?
echo/

rem Create the "lang" array at same time that the menu is displayed
echo 0. None (Clear Registry)

set "n=0"
for %%a in ("American English:en_US"
            "Czech (Czechia):cs_CZ"
            "Danish (Denmark):da_DK"
            "German (Germany):de_DE"
            "European (Spanish):es_ES"
            "Finnish (Finland):fi_FI"
            "French (France):fr_FR"
            "Italian (Italy):it_IT"
            "Japanese (Japan):ja_JP"
            "Korean (South Korea):ko_KR"
            "Dutch (Netherlands):nl_NL"
            "Norwegian (Norway):no_NO"
            "Polish (Poland):pl_PL"
            "Portuguese (Brazil):pt_BR"
            "Russian (Russia):ru_RU"
            "Swedish (Sweden):sv_SE"
            "Chinese (Taiwan):zh_TW"    ) do (
   for /F "tokens=1,2 delims=:" %%b in (%%a) do (
      set /A n+=1
      echo !n!. %%b
      set "lang[!n!]=%%c"
   )
)

echo/
set "choice="
set /P "choice=Please type the number correctly: "
if not defined lang[%choice%] goto XXXX
if "%choice%"=="0" goto clearLang
set "lang=!lang[%choice%]!"

cls
::=================================================================
:changeLang
(
    echo REGEDIT4
    echo/
    echo [HKEY_LOCAL_MACHINE\SOFTWARE\Maxis\The Sims 4]
    echo "Locale"="%lang%"
    echo/
    echo [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Maxis\The Sims 4]
    echo "Locale"="%lang%"
    echo/
) > "%temp%\language.reg"

regedit /S "%temp%\language.reg" >nul
"%windir%\regedit.exe" /S "%temp%\language.reg" >nul
del /F /Q "%temp%\language.reg" >nul
cls

::=================================================================
:finalStep
echo The Sims 4 language changed to %lang%!
pause
exit

::=================================================================
:clearLang
reg delete "HKLM\SOFTWARE\Maxis\The Sims 4" /F
reg delete "HKLM\SOFTWARE\Wow6432Node\Maxis\The Sims 4" /F
cls
echo The Sims 4 registry data cleared!
pause
exit
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thank you very much. However, this script is a bit confusing for me as a beginner, so I think I will use the other answer. Really apreciate it :) – Andhika Aug 13 '18 at 11:02
0

I would clear the variable lang before the if queries and put if not defined lang afterwards, like this:

::=================================================================
:startProg
@echo off
title The Sims 4 Language Changer
cls
::=================================================================
:menuList
echo This program will help you to change The Sims 4 language
echo Please run this program with Administrative Privileges!
echo Anyway, what is your choice, captain?
echo/
echo 0.  None (Clear Registry)
echo 1.  American English
echo 2.  Czech (Czechia)
echo 3.  Danish (Denmark)
echo 4.  German (Germany)
echo 5.  European Spanish
echo 6.  Finnish (Finland)
echo 7.  French (France)
echo 8.  Italian (Italy)
echo 9.  Japanese (Japan)
echo 10. Korean (South Korea)
echo 11. Dutch (Netherlands)
echo 12. Norwegian (Norway)
echo 13. Polish (Poland)
echo 14. Portuguese (Brazil)
echo 15. Russian (Russia)
echo 16. Swedish (Sweden)
echo 17. Chinese (Taiwan)
echo/
set "lang="
set "choice="
set /P choice="Please type the number correctly: "
if "%choice%"=="0" goto :clearLang
if "%choice%"=="1"  set "lang=en_US"
if "%choice%"=="2"  set "lang=cs_CZ"
if "%choice%"=="3"  set "lang=da_DK"
if "%choice%"=="4"  set "lang=de_DE"
if "%choice%"=="5"  set "lang=es_ES"
if "%choice%"=="6"  set "lang=fi_FI"
if "%choice%"=="7"  set "lang=fr_FR"
if "%choice%"=="8"  set "lang=it_IT"
if "%choice%"=="9"  set "lang=ja_JP"
if "%choice%"=="10" set "lang=ko_KR"
if "%choice%"=="11" set "lang=nl_NL"
if "%choice%"=="12" set "lang=no_NO"
if "%choice%"=="13" set "lang=pl_PL"
if "%choice%"=="14" set "lang=pt_BR"
if "%choice%"=="15" set "lang=ru_RU"
if "%choice%"=="16" set "lang=sv_SE"
if "%choice%"=="17" set "lang=zh_TW"
if not defined lang goto :startProg
cls
::=================================================================
:changeLang
> "%temp%\language.reg" (
    echo REGEDIT4
    echo/
    echo [HKEY_LOCAL_MACHINE\SOFTWARE\Maxis\The Sims 4]
    echo "Locale"="%lang%"
    echo/
    echo [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Maxis\The Sims 4]
    echo "Locale"="%lang%"
    echo/
)
regedit /S "%temp%\language.reg" > nul
"%windir%\regedit.exe" /S "%temp%\language.reg" > nul
del /F /Q "%temp%\language.reg" > nul
cls
::=================================================================
:finalStep
echo The Sims 4 language changed to %lang%!
pause
exit /B
::=================================================================
:clearLang
reg delete "HKLM\SOFTWARE\Maxis\The Sims 4" /F
reg delete "HKLM\SOFTWARE\Wow6432Node\Maxis\The Sims 4" /F
cls
echo The Sims 4 registry data cleared!
pause
exit /B

In addition, I changed the following:

  • the quoted set syntax is used, like `set "VAR=Value", to protect special characters and to avoid unintended invisible trailing white-spaces;
  • to output an empty line, echo/ is used, because echo. could fail if a file echo. (no extension) exists;
  • instead of multiple >> redirections, a single > one is used, so a single I/O operation is performed;
  • exit has been replaced by exit /B in order to quit the batch file but not the hosting cmd instance;
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Thanks, this is what I am looking for! I feel a bit stupid because I didn't realize that setting up `if not defined lang goto :startProg` will solve the problem. – Andhika Aug 13 '18 at 10:58