Set encoding for batch file

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Aug 3, 2017
11
1
#1
Is there a way to set the code page for a certain batch file?

I have all those old batch files from 4NT/4DOS times that are written with CP 437 and the umlauts come out wrong. Sure, I could switch to CP 850 now, but I'd rather have an option that describes what CP a batch file is in, thus allowing for good internationalisation.

Python as a blueprint come to mind: https://www.python.org/dev/peps/pep-0263/

Any other scheme will do as well... ;)
 
Aug 3, 2017
11
1
#3
Sorry, I expressed myself badly. I don't want to change the code page of the system. I want to specify the encoding of the batch file.
 
#4
CHCP doesn't change the code page of the system; it changes the code page of TCC itself (and its child processes). If TCC is reading the batch file, I suppose you want to do exactly what David said. You can get the current code page at the beginning of the batch file, and restore it at the end. For example, if TCC is normally using some other code page and the batch file wants to be interpreted by code page 850, you could
Code:
set oldcp=%@word[3,%@execstr[chcp]]
chcp 850
rem more of batch file
chcp %oldcp
quit
But you'd have to worry about the batch file exiting without restoring the code page (error, break, ...).
I don't know how often users need to run a batch file in a code page other than the current one, but two things would make it easier and more foolproof.

1. a _CP variable so one could simply "set oldcp=%_cp"

and/or

2. (better?) a way to specify/change (inside the batch file) the code page for only the duration of the batch file, perhaps "BATCHCP 850", so TCC would revert to the old code page automatically when the batch file exits (for any reason).

Maybe a simple solution would be to make SETLOCAL/ENDLOCAL save/restore the current code page.