Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Set encoding for batch file

Aug
11
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... ;)
 
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.
 
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.
 

Similar threads

Replies
4
Views
2K
Back
Top