how to create a textfile with UTF-8 encoding through vbscript?

寵の児 提交于 2020-01-21 20:21:08

问题


I have written the following code segment:

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

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.

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

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

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?


回答1:


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

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

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



来源:https://stackoverflow.com/questions/25232219/how-to-create-a-textfile-with-utf-8-encoding-through-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!