[Batch] Remove language batch?

Discussion in 'Scripting' started by tcntad, Oct 20, 2010.

  1. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    What about it on the laptop?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. tcntad

    tcntad MDL Guru

    Oct 26, 2009
    4,214
    1,363
    150
    #42 tcntad, Oct 23, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Works fine woho!.

    Code:
    C:\windows\system32>echo en-us
    en-us
    
    C:\windows\system32>echo sv-se
    sv-se
    
    C:\windows\system32>echo zh-tw
    zh-tw
    
    But the code to remove language still doesnt work..

    Code:
    @echo off
    setlocal enabledelayedexpansion
    for /f "tokens=2 delims==" %%A in ('wmic os get oslanguage /format:list') do ( 
    call :dec2hex %%A
    )
    for /f %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\MUI\UILanguages"') do ( 
    for /f "tokens=3" %%a in ('reg query "%%A" /v LCID') do (
    if %%a==%lcid% ( 
    for /f "tokens=7 delims=\" %%b in ("%%A") do (
    set locale=%%b
    )
    )
    )
    )
    for /f "tokens=2,* delims=:" %%A in ('"dism /Online /Get-Packages|find "LanguagePack""') do (
    for /f "tokens=4 delims=~" %%a in ("%%A") do (
    if NOT %locale%==%%a (
    set /p confirm=Do you want to remove %%a?
    if /i %confirm%==y (
    set pkg=%%A
    set pkg=!pkg: =!
    dism /online /Remove-Package /PackageName:!pkg!
    ) ELSE ( 
    echo %%a will not be removed
    )
    )
    )
    )
    pause
    exit
    
    :dec2hex
    setlocal enabledelayedexpansion
    set /a num=%1
    set digits=0123456789ABCDEF
    set ans=
    :loop
    set /a rmd=%num% %% 16
    set /a num /= 16
    set ans=!digits:~%rmd%,1!%ans%
    if NOT %num%==0 goto loop
    endlocal & set lcid=0x%ans%
    exit /b
    
    Still "unexpected (".
     
  3. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    If all the outputs are correct then the script ought to work as expected. It works if the default language is English but it does not if it is another language. I'll test it out and see why you get expected ) error.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. tcntad

    tcntad MDL Guru

    Oct 26, 2009
    4,214
    1,363
    150
    Great:)
    Ill try change to en-US and see if it works.

    I made a small edit, I get "unexpected (" if im not mistaken now. it flashes real quick :p
     
  5. tcntad

    tcntad MDL Guru

    Oct 26, 2009
    4,214
    1,363
    150
    Hm That didnt work too well..
     
  6. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    #46 MasterDisaster, Oct 23, 2010
    Last edited by a moderator: Apr 20, 2017
    This will work
    Code:
    @echo off
    setlocal enabledelayedexpansion
    for /f "tokens=2 delims==" %%A in ('wmic os get oslanguage /format:list') do ( 
    call :dec2hex %%A
    )
    for /f %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\MUI\UILanguages"') do ( 
    for /f "tokens=3" %%a in ('reg query "%%A" /v LCID') do (
    if /i %%a==%lcid% ( 
    for /f "tokens=7 delims=\" %%b in ("%%A") do (
    set locale=%%b
    )
    )
    )
    )
    echo Your current language is %locale%
    for /f "tokens=2,* delims=:" %%A in ('"dism /Online /Get-Packages|find "LanguagePack""') do (
    for /f "tokens=4 delims=~" %%a in ("%%A") do (
    if NOT %locale%==%%a (
    set /p confirm=Do you want to remove %%a?
    if /i !confirm!==y (
    set pkg=%%A
    set pkg=!pkg: =!
    dism /online /Remove-Package /PackageName:!pkg!
    ) ELSE ( 
    echo %%a will not be removed
    )
    )
    )
    )
    pause
    exit
    
    :dec2hex
    setlocal enabledelayedexpansion
    set /a num=%1
    set digits=0123456789ABCDEF
    set ans=
    :loop
    set /a rmd=%num% %% 16
    set /a num /= 16
    set ans=!digits:~%rmd%,1!%ans%
    if NOT %num%==0 goto loop
    endlocal & set lcid=0x%ans%
    exit /b
    
    Condition checking was case sensitive in the previos code, 0x41D was not equal to 0x41d so the locale variable was not set which in turn led to an incomplete IF statement.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. tcntad

    tcntad MDL Guru

    Oct 26, 2009
    4,214
    1,363
    150
    #47 tcntad, Oct 23, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Will try it asap and report back!

    Edit: Now working fine with asking to remove languages and also removing them:)
    Although it says my current language is sv-SE but i switched to en-US hehe.

    Edit2: Ok it is swedish, 7 ultimate didnt feel like actually changing language......
     
  8. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    Good to hear, that it worked as expected.

    The script seems to be a good example for nested for and conditional execution.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. tcntad

    tcntad MDL Guru

    Oct 26, 2009
    4,214
    1,363
    150
    Oyea it did work as expected! :D

    I know its possible but how to make it list the languages installed and ask me which one to remove?
    Annoying? lol.
     
  10. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    #50 MasterDisaster, Oct 23, 2010
    Last edited by a moderator: Apr 20, 2017
    You should have tried it yourself, but here's the solution.

    Code:
    @echo off
    setlocal enabledelayedexpansion
    for /f "tokens=2 delims==" %%A in ('wmic os get oslanguage /format:list') do ( 
    call :dec2hex %%A
    )
    for /f %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\MUI\UILanguages"') do ( 
    for /f "tokens=3" %%a in ('reg query "%%A" /v LCID') do (
    if /i %%a==%lcid% ( 
    for /f "tokens=7 delims=\" %%b in ("%%A") do (
    set locale=%%b
    )
    )
    )
    )
    :menu
    cls
    echo Your current language is %locale%
    echo.
    echo The following languages can be removed from Windows
    echo.
    set /a i=1
    for /f "tokens=2,* delims=:" %%A in ('"dism /Online /Get-Packages|find "LanguagePack""') do (
    for /f "tokens=4 delims=~" %%a in ("%%A") do (
    if NOT %locale%==%%a (
    echo.             !i!. %%a
    set var[!i!]=%%A
    set /a i+= 1
    )
    )
    )
    if %i% EQU 1 (
    echo No additional language packs found
    echo Press any key to exit
    pause>NUL
    exit
    )
    echo.             X. Exit
    echo.
    if %i% GTR 2 (
    set /a i = %i%-1
    set /p choose=Select the language? [1-!i!,X]
    ) ELSE set /p choose=Select the language? [1,X]
    if /i %choose%==x exit
    if %choose% GTR !i! (
    echo Invalid choice 
    goto menu
    )
    set pkg=!var[%choose%]!
    set pkg=!pkg: =!
    dism /online /Remove-Package /PackageName:!pkg!
    pause
    goto menu
    
    :dec2hex
    setlocal enabledelayedexpansion
    set /a num=%1
    set digits=0123456789ABCDEF
    set ans=
    :loop
    set /a rmd=%num% %% 16
    set /a num /= 16
    set ans=!digits:~%rmd%,1!%ans%
    if NOT %num%==0 goto loop
    endlocal & set lcid=0x%ans%
    exit /b
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. tcntad

    tcntad MDL Guru

    Oct 26, 2009
    4,214
    1,363
    150
    #51 tcntad, Oct 23, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Oyea i will try learn more about this stuff, its great:)

    Thanks again for your help!.
     
  12. jykke

    jykke MDL Novice

    Mar 5, 2013
    1
    0
    0
    #52 jykke, Mar 5, 2013
    Last edited by a moderator: Apr 20, 2017
    Nice script! Is there a way to get it work so that the all extra packages would be removed by one dism command?
    like: dism /online /Remove-Package /PackageName:A /PackageName:B /PackageName:C