如何在cmd.exe上正确的Unicode和ANSI输出重定向? [英] How to do proper Unicode and ANSI output redirection on cmd.exe?

查看:2840
本文介绍了如何在cmd.exe上正确的Unicode和ANSI输出重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在Windows上执行自动化,并且您正在重定向不同命令(内部 cmd.exe 或外部)的输出,您会发现您的日志文件包含合并Unicode和ANSI输出(意味着它们无效,并且在查看器/编辑器中无法加载)。



是否可以使cmd.exe工作UTF-8?这个问题不是关于显示,关于stdin / stdout / stderr重定向和Unicode。



我正在寻找一个解决方案,您可以:




  • 使用UTF-8将内部命令的输出重定向到文件

  • 将支持Unicode的外部命令的输出重定向到文件,但编码为UTF-8。



如果不可能获得这种一致性使用批处理文件,是否有另一种方法来解决这个问题,就像使用python脚本为此?在这种情况下,我想知道是否可以单独进行Unicode检测(用户使用脚本不应该记住,如果调用工具将输出Unicode或不输出Unicode,它只是期望将输出转换为UTF-8。



为简单起见,我们假设如果工具输出不是Unicode它将被视为UTF-8(无代码页转换)。

解决方案

可以使用 chcp 更改活动代码页。这将用于重定向文本:

  chcp 65001 
cmd 以<$ c $ 开头,那么这将没有任何效果。p $ p>

< c> / u
开关,强制Unicode(在这种情况下为UTF-16)重定向输出。如果该开关处于活动状态,那么无论使用 chcp 设置的代码页如何,所有输出都将为UTF-16LE。



另请注意,当设置为Raster Fonts时,控制台将无法用于交互式输出。在这种情况下,我会收到有趣的错误讯息:

  C:\Users\JohannesRössel\Documents> x 
活动代码页:65001

系统无法写入指定的设备。

因此,使用一个合理的设置(控制台的TrueType字体)以交互方式使用控制台并具有包含非ASCII字符的路径。


If you are doing automation on windows and you are redirecting the output of different commands (internal cmd.exe or external, you'll discover that your log files contains combined Unicode and ANSI output (meaning that they are invalid and will not load well in viewers/editors).

Is it is possible to make cmd.exe work with UTF-8? This question is not about display, s about stdin/stdout/stderr redirection and Unicode.

I am looking for a solution that would allow you to:

  • redirect the output of the internal commands to a file using UTF-8
  • redirect output of external commands supporting Unicode to the files but encoded as UTF-8.

If it is impossible to obtain this kind of consistence using batch files, is there another way of solving this problem, like using python scripting for this? In this case, I would like to know if it is possible to do the Unicode detection alone (user using the scripting should not remember if the called tools will output Unicode or not, it will just expect to convert the output to UTF-8.

For simplicity we'll assume that if the tool output is not-Unicode it will be considered as UTF-8 (no codepage conversion).

解决方案

You can use chcp to change the active code page. This will be used for redirecting text as well:

chcp 65001

Keep in mind, though, that this will have no effect if cmd was started with the /u switch which forces Unicode (UTF-16 in this case) redirection output. If that switch is active then all output will be in UTF-16LE, regardless of the codepage set with chcp.

Also note that the console will be unusable for interactive output when set to Raster Fonts. I'm getting fun error messages in that case:

C:\Users\Johannes Rössel\Documents>x
Active code page: 65001

The system cannot write to the specified device.

So either use a sane setup (TrueType font for the console) or don't pull this stunt when using the console interactively and having a path that contains non-ASCII characters.

这篇关于如何在cmd.exe上正确的Unicode和ANSI输出重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆