如果调用chcp,则不执行批处理脚本

And*_*ndy 7 unicode cmd batch-file

我正在尝试使用批处理脚本删除一些带有unicode字符的文件(这是一项要求).所以我运行cmd并执行:

> chcp 65001
Run Code Online (Sandbox Code Playgroud)

有效地将代码页设置为UTF-8.它有效:

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
Run Code Online (Sandbox Code Playgroud)

然后我rmdir在批处理脚本中放入相同的命令并将其保存为UTF-8编码.但是,当我没有任何操作时,几乎没有任何东西:在这种情况下,甚至回声都不是来自批处理脚本.即使在OEM编码中保存脚本也无济于事.

因此,当我在控制台中将代码页更改为UTF-8时,脚本就会停止工作.有人知道如何解决这个问题吗?

Jen*_*uak 9

如果要在批处理文件中支持unicode,则CHCP在一行上只会中止批处理文件.我建议将CHCP放在需要unicode的每个批处理文件行上,如下所示

chcp 65001 > nul && <real command here>
Run Code Online (Sandbox Code Playgroud)

示例:在我的情况下,我希望在调试时有一个很好的TAIL我的日志文件,但甚至拉丁-1字符的内容都搞砸了.所以这是我的批处理文件,它包装了来自Windows Resource Kit的真正的尾部实现.

@C:\WINDOWS\system32\chcp.com 65001 >nul && tail.exe -f %1
Run Code Online (Sandbox Code Playgroud)

此外,要输出到控制台,您需要设置一个真正的字体,即Lucidia Console.

显然,对于输出到文件,命令行需要以Unicode运行,因此您将按如下方式启动批处理脚本

cmd /u /c <batch file command here>
Run Code Online (Sandbox Code Playgroud)

免责声明:使用Windows Resource Kit在Windows XP sp3上进行测试.