如何通过vbscript创建具有UTF-8编码的文本文件? [英] how to create a textfile with UTF-8 encoding through vbscript?

查看:585
本文介绍了如何通过vbscript创建具有UTF-8编码的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码段:

strVar = "one line that I want to write in the text file with extension .csv"
'Set fso = CreateObject("Scripting.FileSystemObject")
'fso.CreateTextFile(str2)
'Set f = fso.OpenTextFile(str2,2,True)
'f.WriteLine(str4)
'str2 holds the name of the file, say abc.csv //this is a comment

我认为文本文件的默认编码是ANSI。但是,我想将其编码为IUTF-8。我要这样做的原因是,我有一个以UTF-8格式编码的.temp文件要追加到此csv文件中,这就是我需要将该编码进行后期追加的方式。临时文件仅是一个csv文件,只是其扩展名为.temp。
如果我只是按照上述方式创建文本文件,则在追加文件后会出现一些错误。

the default encoding for text files, I think, is ANSI. However, I want to encode it to IUTF-8. the reason I want to do so is that I have a .temp file encoded in UTF-8 format to append to this csv file, and that is how I need the encoding to be post appending. the temp file is a csv file only, just that it has .temp extension. If I just create the textfile as above, after appending there are a couple of errors in the document.

我正在使用以下命令来追加临时文件通过vbscript调用的命令提示符将文件复制到csv文件:

i am using the following command to append the temp file to the csv file via command prompt which is called by the vbscript:

"copy " &str2 & "+" &str1 & " " &str2

这会产生错误。 str1是临时文件名。

this produces the errors. str1 is the temp file name.

如果我可以使用UTF-8编码创建文本文件,则可以正常工作。或者,将临时文件附加到csv之后,如果我可以将csv转换为具有临时文件的属性(包括编码),也应该没问题。

If I can create the text file as with UTF-8 encoding, it would work fine. Alternatively, after I append the temp file to the csv, if I can convert the csv as to have the properties of the temp files, including the encoding, it should be fine too.

有什么想法可以解决这些问题吗?

Any ideas how any one of these could be done?

推荐答案

如果要追加到文件,只需打开文件用于添加

If you want to append to a file, simply open the file for appending:

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\path\to\your.csv", 8, True)
f.WriteLine "text to append"
f.Close

您也不需要先使用 CreateTextFile() OpenTextFile()的3 rd 参数已经解决了这个问题。

You also don't need to use CreateTextFile() first. The 3rd parameter of OpenTextFile() already takes care of that.

就可打印字符而言,ISO-8859-1和 Microsoft在文本文件中称其为 ANSI

As far as printable characters are concerned, there is no difference between ISO-8859-1 and what Microsoft calls "ANSI" in textfiles.

这篇关于如何通过vbscript创建具有UTF-8编码的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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