我们在Team Foundation Server (TFS)中有一个项目,其中有一个非英语字符(š)。当尝试编写一些与构建相关的脚本时,我们偶然发现了一个问题——我们不能将这个字母传递给命令行工具。命令提示符或其他东西会把它弄乱,tf.exe实用程序无法找到指定的项目。
我尝试了不同格式的.bat文件(ANSI, UTF-8,带BOM和不带BOM),以及用JavaScript编写脚本(本质上是Unicode) -但运气不好。如何执行程序并传递一个Unicode命令行?
我们在Team Foundation Server (TFS)中有一个项目,其中有一个非英语字符(š)。当尝试编写一些与构建相关的脚本时,我们偶然发现了一个问题——我们不能将这个字母传递给命令行工具。命令提示符或其他东西会把它弄乱,tf.exe实用程序无法找到指定的项目。
我尝试了不同格式的.bat文件(ANSI, UTF-8,带BOM和不带BOM),以及用JavaScript编写脚本(本质上是Unicode) -但运气不好。如何执行程序并传递一个Unicode命令行?
当前回答
实际上,关键在于命令提示符实际上理解这些非英语字符,只是不能正确地显示它们。
当我在命令提示符中输入包含一些非英语字符的路径时,它显示为“?? ?”?????? ? ? ? ? ?”当您提交命令(cd "???????? ?????”在我的情况下),一切都按照预期工作。
其他回答
从2019年6月开始,使用Windows 10,你将不必更改代码页。
参见“介绍Windows终端”(来自Kayla Cinnamon)和Microsoft/Terminal。 通过使用Consolas字体,将提供部分Unicode支持。
如Microsoft/Terminal issue 387中所述:
There are 87,887 ideographs currently in Unicode. You need all of them too? We need a boundary, and characters beyond that boundary should be handled by font fallback / font linking / whatever. What Consolas should cover: Characters that used as symbols that used by modern OSS programs in CLI. These characters should follow Consolas' design and metrics, and properly aligned with existing Consolas characters. What Consolas should NOT cover: Characters and punctuation of scripts that beyond Latin, Greek and Cyrillic, especially characters need complex shaping (like Arabic). These characters should be handled with font fallback.
一个快速决定。bat文件,如果你的电脑显示你的路径/文件名正确时,你在dos窗口输入:
copy con temp.txt[按Enter] 输入路径/文件名[按Enter] 按Ctrl-Z[按Enter]
这样你就创建了一个。txt文件- temp.txt。在记事本中打开它,复制文本(不要担心它看起来不可读),并粘贴到你的。bat文件中。 在DOS-window中执行以这种方式创建的.bat对mе(西里尔语,保加利亚语)有效。
由于我还没有看到Python 2.7的完整答案,我将概述两个重要步骤和一个非常有用的可选步骤。
You need a font with Unicode support. Windows comes with Lucida Console which may be selected by right-clicking the title bar of command prompt and clicking the Defaults option. This also gives access to colours. Note that you can also change settings for command windows invoked in certain ways (e.g, open here, Visual Studio) by choosing Properties instead. You need to set the code page to cp65001, which appears to be Microsoft's attempt to offer UTF-7 and UTF-8 support to command prompt. Do this by running chcp 65001 in command prompt. Once set, it remains this way until the window is closed. You'll need to redo this every time you launch cmd.exe.
要获得更永久的解决方案,请参考超级用户上的答案。简而言之,在HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor中使用regedit创建一个REG_SZ (String)条目,并将其命名为AutoRun。将其值修改为chcp 65001。如果不想看到命令的输出消息,请使用@chcp 65001>nul。
有些程序在与这种编码进行交互时遇到麻烦,MinGW就是一个显著的例子,它在编译时出现无意义的错误消息而失败。尽管如此,这工作得非常好,并且不会对大多数程序造成错误。
我也有同样的问题(我来自捷克共和国)。我安装了英文的Windows,而且我必须使用共享驱动器上的文件。文件的路径包含捷克特有的字符。
适合我的解决方案是:
在批处理文件中,修改字符集页
批处理文件:
chcp 1250
copy "O:\VEŘEJNÉ\ŽŽŽŽŽŽ\Ž.xls" c:\temp
批处理文件必须保存在CP 1250中。
注意,控制台不会正确地显示字符,但它会理解它们……
在Windows 10 x64机器上,我通过以下方法使命令提示符显示非英语字符:
打开提升命令提示符(以管理员身份运行CMD.EXE)。通过以下方法查询控制台可用的TrueType字体的注册表:
REG query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont"
你会看到如下输出:
0 REG_SZ Lucida Console
00 REG_SZ Consolas
936 REG_SZ *新宋体
932 REG_SZ *MS ゴシック
现在我们需要添加一个TrueType字体,支持你需要的字符,如Courier New。我们通过在字符串名后面加0来实现,所以在这种情况下,下一个字符串将是"000":
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont" /v 000 /t REG_SZ /d "Courier New"
现在我们实现了UTF-8支持:
REG ADD HKCU\Console /v CodePage /t REG_DWORD /d 65001 /f
设置默认字体为Courier New:
REG ADD HKCU\Console /v FaceName /t REG_SZ /d "Courier New" /f
设置字体大小为20:
REG ADD HKCU\Console /v FontSize /t REG_DWORD /d 20 /f
启用快速编辑,如果你喜欢:
REG ADD HKCU\Console /v QuickEdit /t REG_DWORD /d 1 /f