Home > Mobile >  Batch - Get System Language of current running System
Batch - Get System Language of current running System

Time:08-31

I am trying to get the current setted system-language under use of wmic, but i don´t know why my code here is kind of "broken"...
The readback info from wmiccontains a lot of spaces and CRLF´s. Therefore i wanted to cut all that out in the loop, but the syntax is not proper in my example...

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:MAIN
setlocal
call :Get_System_Language "lang"
echo lang=%lang%
pause
exit /b 0

:Get_System_Language
setlocal
::Windows Language Code Identifier
::1031 German Germany
::2057 English United Kingdom
::1033 English United States
set "language=NUL"
set /a num_languages=3
for /f "tokens=1,2 delims==" %%a in ('wmic os get OSLanguage /Value') do (
    if "%%a" equ "OSLanguage" (
        set "item_tmp=%%b"
        set "value=!item_tmp:=!"
        if "!value!" equ "1031" (
            set "language=german_germany"
        )
        if "!value!" equ "2057" (
            set "language=english_british"
        )
        if "!value!" equ "1033" (
            set "language=english_american"
        )
        (endlocal
            if "%~1" neq "" (set "%~1=!language!")
        )
        exit /b 0
    )
)
exit /b 1

Do you see the Problem?

Thank´s a lot ;-)

CodePudding user response:

Split by = then one more loop to remove CRLF

Example:

@echo off
for /f "tokens=2*delims==" %%i in ('wmic os get OSLanguage /value') do for %%f in (%%i) do (
    if "%%~f" == "1031" set "Lang=german_germany"
    if "%%~f" == "2057" set "Lang=english_british"
    if "%%~f" == "1033" set "Lang=english_american"
)
echo %Lang%

CodePudding user response:

Whilst not a direct answer to your question, I have decided to post this as yourself or future users may find it useful in a similar situation.

@For /F "Delims==" %%G In ("(Set $) 2>NUL") Do @Set "%%G="
@For /F "Tokens=1,* Delims=: " %%G In ('
 %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command
 "[CultureInfo]::InstalledUICulture | Select-Object"^
 " LCID, KeyboardLayoutId, Name, DisplayName, ThreeLetterWindowsLanguageName"'
) Do @Set "$%%G=%%H"
@(Set $) 2>NUL
@Pause

The last two lines are included to hopefully display the names of any variables defined, and their values. The first line is to ensure that none of those are predefined before enumerating.

  • Related