Is it possible to use extened ASCII characters in a BAT file?

旧街凉风 提交于 2020-01-02 07:44:13

问题


I have a bunch of dynamically created *.BAT files. These BAT files are used to create folders in a server. Just one line in each BAT file, such as: MKDIR \NetworkShare\abc\123

This "abc\123" string is from a database.

It runs OK for a while to create thousands subfolders on demand until today it stopped creating a special subfolder which has a "close single quote" (Alt + 0146 if typing from dos prompt) in the string.

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

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

I know I can manually type special characters in DOS prompt (by using keyboard Alt + ). But is there a way to properly save this "close single quote" (Alt + 0146) in BAT file so I can execute them dynamically?

The host system is Windows Server 2003 US-English.


回答1:


Thank you for this CHCP 65001 trick. It leads to proper solution:

I took follow steps to resolve the issue:

+++++++++++++++++++

Prepare the BAT Text File (either manually or dynamically)

+++++++++++++++++++

(1) Make the first line blank (this is necessary, because there are hidden chars in the first line for UTF-8 text file)

(2) Put CHCP 65001 as second line

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

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

+++++++++++++++++++

Now it's the CMD.EXE trick

+++++++++++++++++++

(1) Start cmd.exe

(2) open cmd.exe black screen property

(3) make sure the black screen font is "true type" i.e. "TT" like. By default, it is raster font, can not handle special ascii code properly. (This is the key step)

(4) now I can run my BAT to handle those extended ASCII chars properly.




回答2:


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

chcp 65001

Be careful though: Creating folders with non-ASCII letters can break some programs that may rely on older API of libraries, or just assume that all folder and file names are ASCII.



来源:https://stackoverflow.com/questions/9849486/is-it-possible-to-use-extened-ascii-characters-in-a-bat-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!