[Info] Saving current codepage

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

[Info] Saving current codepage

#1 Post by carlos » 01 May 2018 14:56

I'm doing some test on windows in different languages, for save the current codepage, for restore later with chcp command.

I see almost two codes for save the current codepages, but that fails on some languages.

Chcp outputs:

German: ( it have a dot at the end )

Code: Select all

Aktive Codepage: 850.
Chinese Traditional: ( If you look closely it uses other colon symbol :instead of : )

Code: Select all

使用中字碼頁: 950
Chinese Simplified:

Code: Select all

活动的代码页: 936
Japanase:

Code: Select all

現在のコード ページ: 932
Korean:

Code: Select all

활성 코드 페이지: 949
Spanish:

Code: Select all

Página de códigos activa: 850
Russian

Code: Select all

Текущая кодовая страница: 866
Because that the next code snippet:

Not works for chinese traditional because it uses a different colon character

Code: Select all

for /f "tokens=2 delims=:." %%g in ('chcp') do set "_codepage=%%g"
Not works for german because it not remove the dot at the end

Code: Select all

for /f "tokens=*" %%a in ('chcp') do for %%b in (%%a) do set "_codepage=%%b"
This next works (tested) on environments: german, chinese traditional, chinese simplified, japanase, korean, spanish, russian

Code: Select all

for /f "tokens=*" %%a in ('chcp') do for %%b in (%%a) do set "_codepage=%%~nb"
(old code for reference, also tested) :

Code: Select all

for /f "tokens=*" %%a in ('chcp'
) do for %%b in (%%a) do for /f "delims=." %%c in ("%%b"
) do set "_codepage=%%c"
Eventually I will add more supported languages.
Last edited by carlos on 02 May 2018 16:00, edited 7 times in total.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: [Info] Saving current codepage

#2 Post by npocmaka_ » 02 May 2018 01:38

what about mode command?
Itс results are aligned In columns and eventually you'll be able to get the code page using a substring and numbers.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: [Info] Saving current codepage

#3 Post by carlos » 02 May 2018 07:19

Mmm, i don't know the availability of the mode command on different versions of windows. Also, I have tested the routine using chcp for getting the current chcp. If it works, I not see a reason for change it.

The only advantage that I now see of use mode instead chcp is for change the codepage on windows 2000 and xp where the chcp command clear the screen when you change the codepage from a mbcp to dbcp codepage and viceversa, for example if you have the codepage 950 and use chcp 437 it clear the screen. Using mode con cp select=437 will not clear the screen, but will left some character of the screen with ? characters in replace of some asian characters.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: [Info] Saving current codepage

#4 Post by Compo » 02 May 2018 08:24

There's still no need for three 'For' loops carlos, you could use either:

Code: Select all

For /F "Tokens=*" %%A In ('ChCp') Do For %%B In (%%A) Do Set /A "_codepage=%%B" 2>Nul
or even:

Code: Select all

For /F "Tokens=*" %%A In ('ChCp') Do For %%B In (%%A) Do Set "_codepage=%%~nB"

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [Info] Saving current codepage

#5 Post by aGerman » 02 May 2018 09:52

Interesting! Seems I have to update info.bat.

FWIW I already tried to get informations about the behavior of commands from my Chinese and Japanese colleagues. Unfortunately all the company's computers have a US American configuration :(

Steffen

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: [Info] Saving current codepage

#6 Post by carlos » 02 May 2018 11:48

Thanks @Compo. I prefer the second version that you provide. I think is a bad idea silent a message error. I tested the second version and also works ok. I put that on the first message, because is more clean code. Remove the end dot using the name expansion is a elegant technique.
Eventually if in some windows appears at the end of the number codepage other character than dot, i think my version with 3 for should be used instead adding the character to remove as delimiter.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [Info] Saving current codepage

#7 Post by aGerman » 02 May 2018 13:12

Carlos

May I ask you how you did the tests? Did you find the messages in the internet, or did you change the localization on your computer, or did you read the strings out of the MUI files?

Steffen

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: [Info] Saving current codepage

#8 Post by carlos » 02 May 2018 13:55

aGerman wrote:
02 May 2018 13:12
Carlos

May I ask you how you did the tests? Did you find the messages in the internet, or did you change the localization on your computer, or did you read the strings out of the MUI files?

Steffen
@aGerman: I install a real windows 2000 of each specified language on a virtualbox machine, then I save the output of the chcp command on a text file, and also I run testcp.cmd command for check if it capture it correctly. I choose win2000 because the installation is very short and also for the maximum compatibility.

Screenshot :
Image
Last edited by carlos on 02 May 2018 14:12, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [Info] Saving current codepage

#9 Post by aGerman » 02 May 2018 14:10

I see. Not an option for me, unfortunately. Thanks anyway.

During the development of info.bat I also got informations from siberia-man about the output in a Russian environment. I remember there was a non-breaking space used as thousands separator. I wonder if the space next to the colon is still a space in the Russian output.

Steffen

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: [Info] Saving current codepage

#10 Post by carlos » 02 May 2018 15:59

@aGerman I tested on russian, and is ok. I write the output to the first post. I look with a hexadecimal editor and the space next to the colon is a space. Maybe that you mentions applies to other version of windows on russian, like xp.

Screenshot:
Image

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [Info] Saving current codepage

#11 Post by aGerman » 02 May 2018 16:33

That's great, Carlos. Seems like this method is quite reliable :)

Steffen

sst
Posts: 93
Joined: 12 Apr 2018 23:45

Re: [Info] Saving current codepage

#12 Post by sst » 03 May 2018 14:32

The last proposed solution which uses ~n to eliminate the trailing dot is a neat and clean one.

However if one have concerns about probable immediate trailing characters other than dot, maybe something like this can be helpful.

Code: Select all

for /f "tokens=*" %%a in ('chcp') do for %%b in (%%a) do set "_codepage=%%b"
for /f "delims=0123456789" %%a in ("%_codepage%") do call set "_codepage=%%_codepage:%%a=%%"
It can reduce the chance of failure but does not work with "*?^" which are improbable I think.
Also works if nothing comes after number.

for example

Code: Select all

for /f "tokens=*" %%a in ("A hypothetical chcp output: Active code page:  123450+-/.&$#%%@!~\|:[]{}`'_abcd,;") do for %%b in (%%a) do set "_codepage=%%b"
for /f "delims=0123456789" %%a in ("%_codepage%") do call set "_codepage=%%_codepage:%%a=%%"
set _codepage
_codepage=123450
Just a thought

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: [Info] Saving current codepage

#13 Post by Compo » 04 May 2018 05:09

sst, I provided two examples as alternatives, in my initial post, the first method ignored most trailing characters, the second, which is the one carlos preferred, ignored only the trailing dot

I accept that the first method is unlikely to work with a ? too, but it is illogical that a question mark would trail a result, after all it's an answer not a question.

sst
Posts: 93
Joined: 12 Apr 2018 23:45

Re: [Info] Saving current codepage

#14 Post by sst » 04 May 2018 09:30

@Compo,
Oh, I didn't notice SET /A in your first proposed method, However I doubt that SET /A can be considered a filtering tool.

That being said, it is very improbable in any language or locale one can see any of these characters to be appended at the end of numbers. so something like 12345{ is very unlikely or even exotic, although usage of native characters can not be rules out which will fall in to the range of extended characters. I used that example just for demonstration of the result and it definitely was not a proposed replacement for existing method.
"*?" will not work for obvious reasons so I'll take your statement about question mark as your sense of humor.

All in all, I myself prefer your accepted proposed method over mine.

Post Reply