Append vbCrLF to UCS-2 Little Endian

谁说胖子不能爱 提交于 2019-12-13 02:55:28

问题


I'm using vbscript via HP-UFT (former QTP). I'm facing with issue which looks pretty simple but I couldn't fix it.

I have .CSV files exported from some system and there is no finished CRLF in this file.

I need a simple fix to append new line to this file (I know that is possible write-to-another-file workaround) I'm using FileSystemObject like this:

Set objFile = objFSO.OpenTextFile(outFile,8)' (outFile,8, true/false/default)
objFile.Write "test string" & vbCrLf ' and other different combinations

I didn't use ADODB.Stream because it has not append function and I have no need to additional files

When I'm opening file in Notepad after my tries I see empty squares instead CRLF. I think it because file created with UCS-2 Little Endian encoding. I have no such issue with utf-8

PS maybe some more quick fix of system variable is possible? I have found in network that it possible to change default encoding for all created files via some system varibale but din't find it name. My Language in Region and Language -> Administrative -> Language for non-Unicode is English


回答1:


When in doubt, read the documentation:

Syntax

object.OpenTextFile(filename[, iomode[, create[, format]]])

Arguments

[...]
format
Optional. One of three Tristate values used to indicate the format of the opened file (TristateTrue = -1 to open the file as Unicode, TristateFalse = 0 to open the file as ASCII, TristateUseDefault = -2 to open the file as the system default). If omitted, the file is opened as ASCII.

You open the file for appending, but don't specify the encoding, so the interpreter assumes ASCII format. Change the line

Set objFile = objFSO.OpenTextFile(outFile,8)

to

Set objFile = objFSO.OpenTextFile(outFile, 8, False, -1)

and the problem will disappear.



来源:https://stackoverflow.com/questions/36152339/append-vbcrlf-to-ucs-2-little-endian

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