Home > database >  CMD - language query task
CMD - language query task

Time:07-29

I would like to make a batch, which query the OS's language and after it does commands depending the result.

My code is:

@echo off 
reg query "hklm\system\controlset001\control\nls\language" /v Installlanguage 
if %ERRORLEVEL% EQU 0409 GOTO ENGLISH
GOTO GERMANY
:ENGLISH 
echo English
PAUSE
:GERMANY 
echo Germany 
PAUSE

I know if the req query result is 0409--> English, 0407--> Germany. I try with:

reg query "hklm\system\controlset001\control\nls\language" /v Installlanguage 
if %ERRORLEVEL% EQU 0409 (GOTO ENGLISH) 
else %ERRORLEVEL% EQU 0409 (GOTO GERMANY)

But didn't work. Can somebody help me?

I don't know if there is a problem with the if function. Or that cmd cannot read the received value.

CodePudding user response:

well, reg returns an %errorlevel% of zero, if successful or one, if unsuccessful (typo, key or value non-existent).

You need to parse the output with a for /f loop to get the desired number:

for /f "tokens=3" %%a in ('reg query "hklm\system\controlset001\control\nls\language" /v Installlanguage') do set "lang=%%a"
if "%lang%" == "0407" goto :German
if "%lang%" == "0409" goto :English
echo There is no label for %lang%
goto :eof
:ENGLISH 
echo English
PAUSE
goto :eof
:GERMANY 
echo Germany 
PAUSE
goto :eof

You have to force if to compare strings instead of numbers because numbers starting with a zero are treated as octal, which means 0409 is an invalid octal number and will lead to a script-breaking error if treated as a number.

You have to terminate your main code and subroutines with a goto :eof to prevent the parser to "fall through" the following subroutine.

  • Related