不执行批处理脚本,如果CHCP被称为

问题描述:

我试图删除与UNI code字符中的部分文件它们与批处理脚本(这是一个要求)。所以我运行CMD,执行:

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

有效设置codePAGE为UTF-8。和它的作品:

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

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

 Directory of D:\temp\1

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:\temp\1>rmdir Rún

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

 Directory of D:\temp\1

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

然后我把同样的命令rmdir 批处理脚本命令,并将其保存为UTF-8编码。但是,当我运行没有任何反应,几乎一无所有:没有回音,甚至从作品批处理脚本在这种情况下。即使在节约编码OEM脚本没有帮助。

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.

如此看来,当我在控制台改变codePAGE为UTF-8,脚本会停止工作。是否有人知道如何解决这个问题?

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

如果您想在批处理文件支持UNI code,然后CHCP一行本身只是中止该批处理文件。我的建议是把CHCP上需要UNI code如下每个批处理文件行

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>

例子:在我的情况,我想有而我的调试日志文件,一个漂亮的尾巴,但即使Latin-1字符的内容也会搞砸。因此,这里是一个包装从Windows Resource Kit中的真正实施尾我的批处理文件。

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:\WINDOWS\system32\chcp.com 65001 >nul && tail.exe -f %1

另外,输出到控制台,您需要设置一个True Type字体,即Lucidia控制台。

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

和显然是输出到文件的命令行需要以统一code运行,所以你可以按如下脱掉你的批处理脚本

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>

免责声明:测试在Windows XP SP3的Windows资源工具包

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