Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

unicode - Batch script is not executed if chcp was called

I'm trying to delete some files with unicode characters in them with batch script (it's a requirement). So I run cmd and execute:

> chcp 65001

Effectively setting codepage to UTF-8. And it works:

D:emp1>dir
 Volume in drive D has no label.
 Volume Serial Number is 8C33-61BF

 Directory of D:emp1

02.02.2010  09:31    <DIR>          .
02.02.2010  09:31    <DIR>          ..
02.02.2010  09:32               508 1.txt
02.02.2010  09:28                12 delete.bat
02.02.2010  09:20                95 delete.cmd
02.02.2010  09:13    <DIR>          Rún
02.02.2010  09:13    <DIR>          Гуцул Кал?псо
               3 File(s)            615 bytes
               4 Dir(s)  11?576?438?784 bytes free

D:emp1>rmdir Rún

D:emp1>dir
 Volume in drive D has no label.
 Volume Serial Number is 8C33-61BF

 Directory of D:emp1

02.02.2010  09:56    <DIR>          .
02.02.2010  09:56    <DIR>          ..
02.02.2010  09:32               508 1.txt
02.02.2010  09:28                12 delete.bat
02.02.2010  09:20                95 delete.cmd
02.02.2010  09:13    <DIR>          Гуцул Кал?псо
               3 File(s)            615 bytes
               3 Dir(s)  11?576?438?784 bytes free

Then I put the same rmdir commands in batch script and save it in UTF-8 encoding. But when I run nothing happens, literally nothing: not even echo works from batch script in this case. Even saving script in OEM encoding does not help.

So it seems that when I change codepage to UTF-8 in console, scripts just stop working. Does somebody know how to fix that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to have unicode supported in batch file, then CHCP on a line by itself just aborts the batch file. What I suggest is putting CHCP on each batch file line that needs unicode as follows

chcp 65001 > nul && <real command here>

Example: In my case I wanted to have a nice TAIL of my log files while debugging, but the content for even Latin-1 characters was being messed up. So here is my batch file which wraps the real tail implementation from Windows Resource Kit.

@C:WINDOWSsystem32chcp.com 65001 >nul && tail.exe -f %1

In addition, for output to a console, you need to set a true type font, i.e. Lucidia Console.

And apparently for output to a file the command line needs to run as Unicode, so you would kick off your batch script as follows

cmd /u /c <batch file command here>

Disclaimer: Tested on Windows XP sp3 with Windows Resource Kit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...