Can I use extended ASCII characters in a BAT file?

I have a bunch of dynamically generated * .BAT files. These BAT files are used to create folders on the server. Just one line in each BAT file, for example: MKDIR \ NetworkShare \ abc \ 123

This string "abc \ 123" is taken from the database.

It has been working for a while to create thousands of subfolders on demand, until today it stopped creating a special subfolder with a "closed single quote" ( Alt+ 0146when typed from a dos prompt) on the string.

I did some research and found that this "close single quote" is an extended ASCII character. It cannot be saved properly in the ANSI BAT file (eventually as something else). I tried UNICODE and UTF-8 BAT file but it doesn't work.

The only close-close solution is that I tried a binary editor to make sure it is code 146, but code 146 gives me Γ† (ALT-146) not "close single quote" ( Alt+ 0146).

I know I can manually enter special characters at the DOS prompt (using the keyboard Alt+ ). But is there a way to properly store this "closed single quote" ( Alt+ 0146) in a BAT file so that I can execute them dynamically?

The host system is Windows Server 2003 US-English.


source to share

2 answers

Thank you for the CHCP 65001 trick . This leads to the correct solution:

I decided to follow these steps:


Prepare BAT text file (manually or dynamically)


(1) Make the first line empty (this is necessary because there are hidden characters in the first line for a UTF-8 text file)

(2) Place CHCP 65001 as the second line

(3) main line here: MKDIR \ networkshare \ abc (right single quote -> this is a special extended ASCII char) \ 123

(4) make sure the BAT file is saved as UTF-8


Now this is the CMD.EXE trick


(1) Run cmd.exe

(2) open black screen property cmd.exe

(3) make sure the black screen font is "true type" ie. "TT". By default it is a bitmap font, it cannot handle special ascii code correctly. (This is a key step)

(4) I can now start my BAT to handle these extended ASCII characters correctly.



Try changing the code page of your batch file to UTF-8: paste this line at the top of your batch file and save the file as UTF-8:

chcp 65001


Be careful: creating folders with non-ASCII letters may break some programs that might rely on the old library API, or simply assume that all folder and file names are ASCII.



All Articles