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

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

问题描述

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

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).

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

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:

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

如果使用批处理文件无法获得这种一致性,是否有另一种解决此问题的方法,例如为此使用 python 脚本?在这种情况下,我想知道是否可以单独进行 Unicode 检测(使用脚本的用户应该不记得被调用的工具是否会输出 Unicode,它只会期望将输出转换为 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.

为简单起见,我们假设如果工具输出不是 Unicode,它将被视为 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).

推荐答案

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

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

chcp 65001

但是请记住,如果 cmd 是使用 /u 开关启动的,这会强制 Unicode(在这种情况下为 UTF-16)重定向,则这将不起作用输出.如果该开关处于活动状态,则无论使用 chcp 设置的代码页如何,所有输出都将采用 UTF-16LE.

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.

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

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:UsersJohannes RösselDocuments>x
Active code page: 65001

The system cannot write to the specified device.

因此,要么使用合理的设置(控制台的 TrueType 字体),要么在以交互方式使用控制台并具有包含非 ASCII 字符的路径时不要使用此特技.

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天全站免登陆