如何转换存储在UTF-8的东西一个批处理文件,通过另一个批处理文件工作并运行它 [英] How to convert a batch file stored in utf-8 to something that works via another batch file and run it

查看:151
本文介绍了如何转换存储在UTF-8的东西一个批处理文件,通过另一个批处理文件工作并运行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节目,我用它来创建一个批处理文件。我的问题是程序的输出使其尽快像é任何变音符,A,O,A是失败我的批处理文件是UTF-8。
看来我不能想出一个办法,以我的输出转换成任何东西,但UTF-8创建批处理文件的程序。

I have a program I use to create a batch file. My problem is that the program's output is UTF-8 so as soon as any diacritical marks like é,à,ö,Ä are in my batch file it fails. It seems I can't figure out a way to convert my output to anything but UTF-8 in the program that creates the batch file.

所以我在想创建两个巴赫的文件。可以转换从UTF-8到ANSI与实际实际的一个又一个的(WINDOWS codePAGE 1252,或者CP 850),并在那之后执行。当然,我想补充一个CHCP XXXX为实际的批处理文件的第一个命令。

So I was thinking of creating two bach files. The actual one and another that converts the actual one from UTF-8 to ANSI (Windows Codepage 1252, or maybe cp 850) and then executes it after that. Of course I'd add a chcp xxxx as the first command of the actual batch file.

所以我的问题是有没有的iconv在Windows上一个选择 - 或者一个人如何使用第二个批处理文件转换为UTF-8文本文件到Windows codePAGE。是否有内置Win XP的最多,我可以用或者是有一个自由和再发行的工具,我可能会使用这个东西吗?

So my question is is there an alternative to iconv on Windows - or how does one convert a UTF-8 text file to a windows codepage using a second batch file. Is there anything built into Win XP and up that I could use or is there a free and redistributable tool I might use for this?

请注意:

chcp 65001

不批处理文件。

编辑1:

在Windows XP中,我创建两个批处理文件来测试第一个答案。

on windows XP I created two batch files to test the first answer.

1.BAT EN coded到UTF-8无BOM包括:

1.bat encoded to UTF-8 without BOM contains:

chcp 1252
cd üöä

2.bat还设有coded到UTF-8无BOM - 但没有任何特殊字符包括:

2.bat also encoded to UTF-8 without BOM - but without any special characters contains:

chcp 1252
type "1.bat" >"ansi_file.bat"

创建的结果ansi_file.bat当一个执行2.bat仍将是UTF-8 EN codeD,而不是ANSI EN codeD。

The resulting ansi_file.bat created when one executes 2.bat will still be utf-8 encoded and not ansi encoded.

编辑2:

所提到的反向过程的工作。

The mentioned reverse process works.

chcp 1252
echo ü > ansi.txt
cmd /u /c type ansi.txt > unicode.txt

但无论是以下后续行的

but neither of the following subsequent lines

cmd /a /c type unicode.txt > back2ansi.txt
type unicode.txt > back2ansi_v2.txt

让我回ANSI。我想这两者在Win XP和Win 7。
谁能帮助?

gets me back to ANSI. I tried this both on Win XP and Win 7. Can anyone help?

注意:

我知道如何使用Windows脚本宿主和VBS。我想,以避免因脚本主机上虽然。该VBS方法在这里详细描述:<一href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/aa368046%28v=vs.85%29.aspx\">http://msdn.microsoft.com/en-us/library/windows/desktop/aa368046%28v=vs.85%29.aspx

I'm aware of how to use the Windows Script Host and VBS. I'd like to avoid depending on the script host though. The VBS method is detailed here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa368046%28v=vs.85%29.aspx

修改3:

创建了包含以上UNI code U中的文本文件不是UTF-8

The text file created containing a unicode ü above is not utf-8

在Windows UNI code文件是HEX:

The Windows unicode file is HEX:

FC 00 20 00 0D 00 0A 00

UTF-8无BOM是HEX:

UTF-8 without BOM would be HEX:

C3 BC 20 0D 0A

链接到的唯一解决方案VBS工作与UNI code形式,但失败UTF-8表格上。
我需要UTF-8转换为另一种code页面,以便甚至没有人似乎为我工作...

The VBS solution linked to only works with the unicode form but fails on the UTF-8 form. I need to convert UTF-8 to another code page so not even that one seems to work for me...

推荐答案

您已经表示不想依靠脚本主机上,但没有提供原生的批处理命令,可以做你想做的。你将不得不使用的的东西的超出了纯批次。该脚本宿主是原产于Windows,所以我想会不会是一个问题。

You have stated you don't want to rely on the script host, but there is no native batch command that can do what you want. You are going to have to use something beyond pure batch. The script host is native to Windows, so I should think it would not be a problem.

下面的 UTF8toANSI.vbs 脚本转换成UTF-8(带或不带BOM)为ISO-8859-1,(基本等同于code页1252)。它改编自 VB6 / VbScsript更改文件/写编码文件ANSII

The following UTF8toANSI.vbs script converts UTF-8 (with or without BOM) into ISO-8859-1, (basically the same as code page 1252). It is adapted from VB6/VbScsript change file / write file with encoding to ansii.

Option Explicit

Private Const adReadAll = -1
Private Const adSaveCreateOverWrite = 2
Private Const adTypeBinary = 1
Private Const adTypeText = 2
Private Const adWriteChar = 0

Private Sub UTF8toANSI(ByVal UTF8FName, ByVal ANSIFName)
  Dim strText

  With CreateObject("ADODB.Stream")
    .Open
    .Type = adTypeBinary
    .LoadFromFile UTF8FName
    .Type = adTypeText
    .Charset = "utf-8"
    strText = .ReadText(adReadAll)
    .Position = 0
    .SetEOS
    .Charset = "iso-8859-1"
    .WriteText strText, adWriteChar
    .SaveToFile ANSIFName, adSaveCreateOverWrite
    .Close
  End With
End Sub

UTF8toANSI WScript.Arguments(0), WScript.Arguments(1)

的VBS脚本将需要在当前目录或路径。

The VBS script would need to be in your current directory or your path.

一个批处理脚本转换和运行UTF8 EN codeD脚本可以是这个样子:

A batch script to convert and run your UTF8 encoded script could look something like this:

@echo off
UTF8toANSI "utf8.bat" "ansi.bat"
ansi.bat


原来的答复: 下面是我原来的答复与BOM对于UTF-16的作品,而不是UTF-8

内部命令的输出,如果自动输出管道或重定向到一个文件转换为ANSI。

The output of internal commands is automatically converted to ANSI if output is piped or redirected to a file.

chcp 1252
type "utf_file.bat" >"ansi_file.bat"

这个过程可以反向如果CMD启动与 / U 选项,但遗憾的是单向code头字节将丢失去了。但是,当然,这对你的情况是个问题。

The process can go in reverse if CMD is started with the /U option, but unfortunately the unicode header bytes will be missing. But of course that is a non-issue for your situation.

这篇关于如何转换存储在UTF-8的东西一个批处理文件,通过另一个批处理文件工作并运行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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