因此,我最近创建了带有菜单的批处理文件,并允许用户输入0-17之间的数字,但是如果用户输入其他内容(而不是数字0-17),我想将该批处理文件设置为goto startProg
)或输入空值。在我当前的批处理文件中,它将空值和其他输入传递到下一部分(:changeLang
部分)。
我的批处理文件:
::=================================================================
: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
问题是我不想更改:
如果“%choice%” ==“ XX”设置lang = XXXXX
收件人:
如果“%choice%” ==“ XX”转到XXXXX
因为它将通过创建许多goto XXX
节来使我的批处理文件更大。
我也不想创建多个if not
命令,因为它效率不高。
答案 0 :(得分:2)
这是我使用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
答案 1 :(得分:0)
我将在lang
查询之前清除变量if
,然后在其后放置if not defined lang
,如下所示:
::=================================================================
: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
此外,我更改了以下内容:
set
语法(例如`set“ VAR = Value”)来保护特殊字符并避免意外的可见尾随空格; echo/
,因为如果文件echo.
(无扩展名)存在,echo.
可能会失败; >>
重定向,而是使用了一个>
重定向,因此执行了一个I / O操作; exit
已替换为exit /B
,以便退出批处理文件,但不退出托管cmd
实例;