Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unicode 如果调用了chcp,则不会执行批处理脚本_Unicode_Batch File_Cmd - Fatal编程技术网

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

Unicode 如果调用了chcp,则不会执行批处理脚本,unicode,batch-file,cmd,Unicode,Batch File,Cmd,我正试图用批处理脚本删除一些带有unicode字符的文件(这是一项要求)。所以我运行cmd并执行: > chcp 65001 有效地将代码页设置为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.

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

> chcp 65001
有效地将代码页设置为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
D:\temp\1>dir
驱动器D中的卷没有标签。
卷序列号为8C33-61BF
D:\temp\1的目录
02.02.2010  09:31              .
02.02.2010  09:31              ..
02.02.2010 09:32 508 1.txt
02.02.2010 09:28 12删除.bat
02.02.2010 09:20 95 delete.cmd
2010年2月2日09:13劳恩
02.02.2010  09:13              Гуцул Каліпсо
3个文件615字节
4个目录11 576 438 784个可用字节
D:\temp\1>rmdir Rún
D:\temp\1>目录
驱动器D中的卷没有标签。
卷序列号为8C33-61BF
D:\temp\1的目录
02.02.2010  09:56              .
02.02.2010  09:56              ..
02.02.2010 09:32 508 1.txt
02.02.2010 09:28 12删除.bat
02.02.2010 09:20 95 delete.cmd
02.02.2010  09:13              Гуцул Каліпсо
3个文件615字节
3个目录11 576 438 784个可用字节
然后我将相同的
rmdir
命令放入批处理脚本中,并将其保存为UTF-8编码。但当我运行任何操作时,实际上什么也没有发生:在本例中,甚至连echo都不能从批处理脚本中工作。即使以OEM编码保存脚本也没有帮助


所以,当我在控制台中将代码页更改为UTF-8时,脚本似乎停止工作。有人知道如何解决这个问题吗?

控制台中的Unicode支持,特别是在批处理文件中,非常糟糕。 你能“扭转”要求说PowerShell或活动脚本(VBScript或JScript)吗

从长远来看,这将为你节省大量的悲伤(如果你需要在这个简单的任务之外增加这一点)


更不用说PowerShell和ActiveScripting都使用更强大的语言,允许函数、正确的循环、实变量、调试器,以及许多用于更严肃项目的好东西。

如果您想在批处理文件中支持unicode,那么一行上的CHCP本身只会中止批处理文件。我建议将CHCP放在需要unicode的每个批处理文件行上,如下所示

chcp 65001 > nul && <real command here>
cmd /u /c <batch file command here>
此外,对于控制台的输出,您需要设置一个true type字体,即Lucidia控制台

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

chcp 65001 > nul && <real command here>
cmd /u /c <batch file command here>
cmd/u/c

免责声明:使用Windows资源工具包在Windows XP sp3上测试。

尝试在批处理文件的第一行插入一个空行

@C:\WINDOWS\system32\chcp.com 65001 >nul && tail.exe -f %1
第1行:

第2行:CHCP 65001

第3行:脚本命令


应该有用

思考并很快写出一个实际的答案,但有两个问题:您是否确保批处理文件在开始时没有U+FEFF?你能用VBScript代替批处理文件吗?Unicode支持在那里容易得多。不,批处理文件中没有BOM表。我可以使用VBS,但我只是想知道我的问题是否可以直接解决;不久前,我在批处理文件中使用Unicode取得了一些进展,但它一点也不漂亮,充其量也不完整。是的,我可以。我只是想知道,有没有可能直接解决这个bug(事实上,它是个bug,不是吗?)那么也许
chcp 65001&&path\to\batchfile.bat
也可以工作?对不起,我忘了:确保bat文件保存为UTF-8为什么这样做?如果可能的话,请通过添加测试用例进行扩展。对于Win7,这是可行的,但是对于chcp 65001,无论是否有空行,您都不会遇到任何问题。对于XP,这仍然失败。