如何在 cmd.exe 上进行正确的 Unicode 和 ANSI 输出重定向?
如果您在 Windows 上进行自动化,并且重定向不同命令的输出(内部 cmd.exe
或外部),您会发现您的日志文件包含组合的 Unicode 和 ANSI 输出(这意味着它们是无效并且无法在查看器/编辑器中很好地加载)
是否可以使 cmd.exe 使用 UTF-8? 这个问题与显示无关,而是关于 stdin/stdout/stderr 重定向和 。
我正在寻找一种解决方案,允许您:
- 使用 UTF-8 将内部命令的输出重定向到文件
- ,则将支持 Unicode 的外部命令的输出重定向到文件
如果不可能 使用批处理文件获得这种一致性,是否有另一种方法来解决这个问题,例如使用 python 脚本来解决这个问题?在这种情况下,我想知道是否可以单独进行 Unicode 检测(使用脚本的用户应该这样做)不记得调用的工具是否会输出 Unicode,它只是期望将输出转换为 UTF-8。
为简单起见,我们假设如果工具输出不是 Unicode,它将被视为 UTF-8(无代码页转换)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 chcp 更改活动代码页。这也将用于重定向文本:
但是请记住,如果
cmd
使用强制 Unicode (UTF) 开关启动cmd
,这将不起作用在本例中为 -16)重定向输出。如果该开关处于活动状态,则无论chcp
设置的代码页如何,所有输出都将采用 UTF-16LE 格式。另请注意,当设置为光栅字体时,控制台将无法用于交互式输出。在这种情况下,我收到有趣的错误消息:
因此,要么使用合理的设置(控制台的 TrueType 字体),要么在交互式使用控制台且路径包含非 ASCII 字符时不要使用此特技。
You can use
chcp
to change the active code page. This will be used for redirecting text as well: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 withchcp
.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:
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.
没有
帮助我。这样我在打印警告中就有了广泛的特征。
without
Helped me. With that i had wide character in print warning.