批处理文件编码 [英] Batch file encoding

查看:529
本文介绍了批处理文件编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理包含文件名奇怪的字符,如法国é。

I would like to deal with filename containing strange characters, like french é.

一切都在外壳做工精细:

Everything is working fine in the shell :

C:\somedir\>ren -hélice hélice

知道如果我把这个行.bat文件,我得到以下结果:

Know if I put this line in a .bat file, I obtain the following result :

C:\somedir\>ren -hÚlice hÚlice

看到了吗? é已取代U

See ? é have been replaced by Ú

同样是命令的输出如此。如果我 DIR 一些目录中的外壳,输出是好的。
如果我这个输出重定向到一个文件中,出现一些字符转化。

The same is true for command output. If I dir some directory in the shell, output is fine. If I redirect this output to a file, some character are transformed.

所以你怎么能告诉我怎么CMD.EXE什么出现在我的批处理文件的电子间preT,确实是一个电子而不是U或逗号

So how can I tell cmd.exe how to interpret what appears as an é in my batch file, is really an é and not a Ú or a comma

编辑:
因此,有执行.bat文件时,提供有关在其被写入codePAGE的暗示没有办法?

Edit : So there is no way when executing a .bat file to give an hint about the codepage in which it was written ?

推荐答案

您必须保存与OEM编码批处理文件。如何做到这一点取决于你的文本编辑器而异。在这种情况下,所使用的编码而变化为好。对于西方文化中它通常是CP850。

You have to save the batch file with OEM encoding. How to do this varies depending on your text editor. The encoding used in that case varies as well. For Western cultures it's usually CP850.

批处理文件和编码真的是两件事情,特别不喜欢对方。你会发现,统一code也不可能用在那里,不幸的是(即使环境变量处理得很好)。

Batch files and encoding are really two things that don't particularly like each other. You'll notice that Unicode is also impossible to use there, unfortunately (even though environment variables handle it fine).

另外,你可以设置控制台使用另一个codePAGE:

Alternatively, you can set the console to use another codepage:

chcp 1252

应该做的伎俩。至少它为我工作在这里。

should do the trick. At least it worked for me here.

当你做输出重定向,如 DIR ,适用同样的规则。控制台窗口的codePAGE使用。您可以使用 / U 开关置于的cmd.exe 来强制统一code输出重定向,这导致生成的文件是在UTF-16。

When you do output redirection, such as with dir, the same rules apply. The console window's codepage is used. You can use the /u switch to cmd.exe to force Unicode output redirection, which causes the resulting files to be in UTF-16.

至于普遍在的cmd.exe 编码和code页面,也看到了这个问题:

As for encodings and code pages in cmd.exe in general, also see this question:

  • What encoding/code page is cmd.exe using

编辑:至于你的编辑:没有, CMD 总是假定写入控制台默认codePAGE该批处理文件。但是,你可以很容易地包括 CHCP 在批的启动:

As for your edit: No, cmd always assumes the batch file to be written in the console default codepage. However, you can easily include a chcp at the start of the batch:

chcp 1252>NUL
ren -hélice hélice

为了使这更健壮直接在命令行中使用时,您可能需要记住老code网页事后恢复它:

To make this more robust when used directly from the commandline, you may want to memorize the old code page and restore it afterwards:

@echo off
for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
chcp 1252>nul
ren -hélice hélice
chcp %cp%>nul

这篇关于批处理文件编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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