开发者

Write Chinese chars to a text file using vbscript

开发者 https://www.devze.com 2022-12-19 12:55 出处:网络
I\'m trying to write some Chinese characters to a text file using Set myFSO = CreateObject(\"Scripting.FileSystemObject\")

I'm trying to write some Chinese characters to a text file using

Set myFSO = CreateObject("Scripting.FileSystemObject")
Set outputFile = myFSO.OpenTextFile(getOutputName(Argument, getMsiFileName(Wscript.Arguments)), forWriting, True)
outputFile.WriteLine(s) 

The variable s contains a Chinese character that I read from the other file. I echo s value and I can 开发者_Go百科see the s correctly in the screen. However, for some reason the script stops running after outputFile.WriteLine(s) without returning any error message.

Am I missing something?


Maybe it's got something to do with character encoding. Try directly specifying the Unicode format for the file in the last parameter of the OpenTextFile method:

Const Unicode = -1
Set outputFile = myFSO.OpenTextFile(getOutputName(Argument, getMsiFileName(Wscript.Arguments)), forWriting, True, Unicode)

Also, you need to close the file after writing to it:

outputFile.Close

If this doesn't help, try error handling like AnthonyWJones suggested.


Try this:-

MsgBox "Writing Line"
On Error Resume Next
outputFile.WriteLine s '' # Removed ( ) that shouldn't be there.
MsgBox "Err " & Err.Number & ": " & Err.Description
On Error GoTo 0

What do you get?


Things had changed along win versions. This works on Win10:

Set outputfile = myFSO.CreateTextFile(filename,True,True)

The 3rd arg is bool true/false for unicode/ascii 2. Surprisingly, when using myFSO.OpenTextFile the arg is int with 1 (not -1) for unicode 1.

Documentation:

https://msdn.microsoft.com/en-us/library/aa265018(v=vs.60).aspx

https://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消