批处理文件-仅一个SET命令,但具有多个变量

时间:2018-08-21 14:36:01

标签: batch-file command

我想制作一个批处理文件,以检测目录中是否存在名为lang-XX.txt的文件,该XX将是语言代码,例如EN(英语),FR(法国)等。

但是,我不想使用多个if exist lang-XX.txt命令,例如:

if exist lang-US.txt goto launchGame
if exist lang-FR.txt goto launchGame
if exist lang-DE.txt goto launchGame
if exist lang-IT.txt goto launchGame
if exist lang-JP.txt goto launchGame
if exist lang-PL.txt goto launchGame
if exist lang-RU.txt goto launchGame
if exist lang-ES.txt goto launchGame
rem This will set game language and launch the game on the 'launchGame' section

所以,我想像这样:

set "lang=US=FR=DE=IT=JP=PL=RU=ES"
if exist lang-%lang%.txt goto launchGame

但是,由于存在多个lang变量,因此它当然不起作用,并且键入echo %lang%将得到US=FR=DE=IT=JP=PL=RU=ES。那么,如何使它起作用?

这是我当前的完整批处理文件:

@echo off
title Saints Row IV Launcher

:checkLang
set "lang=US=FR=DE=IT=JP=PL=RU=ES"
if exist lang-%lang%.txt goto launchGame

:startMenu
echo/
echo This is a launcher + language changer for Saints Row IV.
echo Your selected language will be saved to "lang-XX.txt" file.
echo/
echo This launcher will read from it so you don't need to change the
echo language again every time you launch the game using this launcher.
echo Please make sure that you have write access to this directory!
echo/
echo Avaiable languages:
echo 1) English
echo 2) French
echo 3) German
echo 4) Italian
echo 5) Japanese
echo 6) Polish
echo 7) Russian
echo 8) Spanish
echo/
set "lang="
set /P "menu=Please type the number correctly: "
if not defined %lang% goto checkLang
if "%menu%"=="1" set lang=US
if "%menu%"=="2" set lang=FR
if "%menu%"=="3" set lang=DE
if "%menu%"=="4" set lang=IT
if "%menu%"=="5" set lang=JP
if "%menu%"=="6" set lang=PL
if "%menu%"=="7" set lang=RU
if "%menu%"=="8" set lang=ES

:launchGame
rem Will detect if 'lang-XX.txt' file existed, if not, make a new 'lang-XX.txt' file
if not exist lang-%lang%.txt type nul >lang-%lang%.txt

rem Start the game with %lang% parameter...
start "" "Saints Row IV.exe" -localize_language %lang%

如果可能的话,如果一个目录中存在多个lang-XX.txt文件(例如:lang-EN.txt和lang-JP.txt),如何使批处理文件显示错误或{{1 }}而不是继续执行该过程?

1 个答案:

答案 0 :(得分:0)

这听起来像您需要一个for循环来遍历各种语言-this question-包含有关如何在批处理脚本中执行操作的详细信息。

for %%x in (US FR DE IT JP PL RU ES) do (
    if exist lang-%%x.txt (
         set "lang=%%x"
         goto launch_game
    )
)