1

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

Vitaliy
  • 359
  • 1
  • 7
  • 16
  • *"didn't use `ADODB.Stream` because it has not append function"*, is that so? What do you call `Write()`, just move to the `EOS` first using the `Position` property. – user692942 Mar 22 '16 at 10:52
  • You can't use `vbCrLf` because the VBScript isn't UCS-2 so will produce `0D0A` when you actually want `0D00 0A00` for [Carriage Return](http://www-01.ibm.com/software/globalization/ccsid/control_cr.html) [LineFeed](http://www-01.ibm.com/software/globalization/ccsid/control_lf.html). – user692942 Mar 22 '16 at 11:00
  • Read in the textfile, append your text, write new text over existing file. –  Mar 22 '16 at 11:02
  • Microsoft uses both terms (UCS-2 and UTF-16) for the same encoding. Technically it might not be entirely correct, but it's close enough so that the differences don't matter in this context. [[Wikipedia](https://en.wikipedia.org/wiki/UTF-16)] – Ansgar Wiechers Mar 22 '16 at 12:09

1 Answers1

2

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    Good answer, but the term [`unicode` is ambiguous](http://stackoverflow.com/a/700221/692942) it means one thing to one system and one to another could be `UTF-8` or `UTF-16` depending on system defaults. – user692942 Mar 22 '16 at 11:31
  • Should be ok though as [Windows uses `UTF-16`](https://msdn.microsoft.com/en-us/library/windows/desktop/dd374081(v=vs.85).aspx). – user692942 Mar 22 '16 at 11:40
  • 1
    UTF-16 LE to be precise. I agree that in general the term "Unicode" is ambiguous, but when it comes to VBScript it always refers to UTF-16 LE. – Ansgar Wiechers Mar 22 '16 at 11:59
  • Strange I just tried a test script on Windows 8.1 using `CreateTextFile(filepath, True)` and get `0D0A` when opening it with a HEX editor, shouldn't it be `0D00 0A00`? – user692942 Mar 22 '16 at 12:09
  • 1
    @Lankymart For the [`CreateTextFile`](https://msdn.microsoft.com/en-us/library/5t9b5c0c%28v=vs.84%29.aspx) method the third parameter defines the format, not the second one. Default is ASCII (same as with `OpenTextFile`). Use `CreateTextFile(filepath, True, True)` and you'll get the expected result. – Ansgar Wiechers Mar 22 '16 at 12:12
  • Damn, was typing to fast your right that was it now produces `0D00 0A00`. – user692942 Mar 22 '16 at 12:14
  • Thanks for your example, I forgot to use for OpenTextFile fourth parameter, so all my tries was like: OpenTextFile(outFile,8, ...). It's only one problem that this code will run on machines with the different locale, but currently it used only by me :) – Vitaliy Mar 22 '16 at 12:34