批量文件编码 [英] Batch file encoding

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

问题描述

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

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

一切都在shell中正常工作:

Everything is working fine in the shell:

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

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

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

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

看到? é已被Ú替换。

See ? é have been replaced by Ú.

命令输出也是如此。如果我在shell中的某个目录 dir ,输出结果是正确的。如果我将这个输出重定向到一个文件,一些字符被转换。

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

那么我怎么能告诉cmd.exe如何解释在我的批处理文件中显示为é,真的是一个é而不是Ú或逗号?

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文件时,没有办法给出一个关于它写的代码页的提示?

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.

批处理文件和编码实际上是两件并不特别相似的东西。你会注意到Unicode也不可能在那里使用,不幸的是(即使环境变量处理得很好)。

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

或者,您可以将控制台设置为使用另一个代码页:

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

chcp 1252

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

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

当您执行输出重定向时,例如使用 dir ,相同的规则应用。使用控制台窗口的代码页。您可以使用 / u 切换到 cmd.exe 来强制执行Unicode输出重定向,这会导致生成的文件为在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 中的编码和代码页,一般来说,也可以看到这个问题:

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

  • What encoding/code page is cmd.exe using

编辑: / strong>关于编辑:否, cmd 总是假定批处理文件被写入控制台默认代码页。但是,您可以在批次开始时轻松添加 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

直接从命令行中,您可能需要记住旧代码页,然后恢复:

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