0

I want to make a batch file that detects if a file named lang-XX.txt exist in a directory, the XX will be a language code like EN (english), FR (france), etc.

However, I do not want to make it using multiple if exist lang-XX.txt command like:

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

So, I want to make it like:

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

But of course, it will not work since there are multiple lang variable and typing echo %lang% will result US=FR=DE=IT=JP=PL=RU=ES. So, how do I make it works?

Here are my current full batch file:

@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%

And if possible, if multiple lang-XX.txt file existed on one directory (example: lang-EN.txt and lang-JP.txt), how do I make the batch file to show an error or goto XXXXX instead of continuing the process?

Andhika
  • 5
  • 6
  • Please note that the first %lang% in `if not exist lang-%lang%.txt type nul >lang-%lang%.txt` would be any of the 8 languages (EN, FR, DE, etc) but the second %lang% (in `type nul >lang-%lang%.txt`) would be only one (not all 8 languages). I think you will need to separate this command into 2 commands (or more) – Andhika Aug 21 '18 at 14:45
  • 1
    What about this: `for %%I in (US FR DE IT JP PL RU ES) do if exist "lang-%%I.txt" goto launchGame`? – aschipfl Aug 21 '18 at 14:45
  • So.. If I `set lang=%%I` will it use one of the language that was detected using `if exist "lang-%%I.txt` or will results an error? And what if there are 2 lang-XX.txt file? (my second question is optional to answer) – Andhika Aug 21 '18 at 14:54
  • Yes, `set "lang=%%I"` would set `lang` to the first language code found. My code just replaces your first code fragment, it does not detect whether there are more than one language files, but this could be done, like this: `set "lang=" & for %%I in (US FR DE IT JP PL RU ES) do if exist "lang-%%I.txt" if defined lang (goto :someErrorRoutine) else set "lang=%%I"`, then `if defined lang (goto :launchGame) else rem no language defined`. – aschipfl Aug 21 '18 at 15:05
  • What is the command `if defined lang (goto :someErrorRoutine)` for? Because I think you already set it to NULL on the beginning so it will be always not defined (until the next `set lang=%%I` command) – Andhika Aug 21 '18 at 15:23
  • To detect whether more than one language file exists; `goto :someErrorRoutine` is of course just a place-holder here and must be replaced with some useful code (for example, echoing an error message)... – aschipfl Aug 21 '18 at 15:25
  • How did the answer below solve your issue as far as user choice is concerned? It just loops through each language and if the file exists launches it. But what if the user wants a different language? – Gerhard Aug 21 '18 at 16:07
  • I would suggest you to change the logic. Instead of encoding the language preference information in file name like `lang-xx.txt `, use constant file name like `lang.txt` and save the language preference code in the file contents. something like `(echo US)>lang.txt` then read it back by `set /p "lang=" – sst Aug 21 '18 at 20:22
  • @GerhardBarnard I'm assuming that there are no 'lang-XX.txt' file in the directory before the user opens this batch file. Also, logically, most people will only choose their own language (not trying multiple languages) so it will only require one time input from the user. If the user needs to change to other language again, then he/she needs to delete the 'lang-XX.txt' file – Andhika Aug 22 '18 at 01:30
  • @sst I already thought of that, but.. I didn't find a command which do something like that without requiring the same command repetition. For example like this [one](https://stackoverflow.com/a/13476936/4873027), it will requires multiple `findstr` commands which is not efficient. Do you have a workaround for this? – Andhika Aug 22 '18 at 01:46
  • @sst Nevermind, I found how to do it. Thanks, really apreciate your help. – Andhika Aug 22 '18 at 02:14

1 Answers1

0

This sounds like you need a for loop to loop through the languages - this question - has details about how to do it in a batch script.

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