Append vbCrLF to UCS-2 Little Endian

2019-08-15 06:35发布

问题:

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.