2

I am reading and writing a file in VBScript.

My input file starts with these five characters: <?xml. I verified those 5 starting characters with a hex editor. The input file is a DITA map, such as what is shown here: DITA map explanation.

My output file starts with a BOM of hex FF FE, so when I try to use (read) that output file as my input file, it barfs, and I get errors. Where is this FF FE coming from, and how do I stop it from getting generated?

Another difference I saw in the hex editor is that the input file is normal one byte per character. But, the hex editor shows the output file has 00 before each character.

I started out using .ReadLine and .WriteLine, but switched to .ReadAll and .Write thinking that might solve my problem, but it did not.

I researched BOM and VBScript but found no solutions.

Set FileIn = FSO.OpenTextFile("C:\foo\barIn.txt", 1)
Text = FileIn.ReadAll
FileIn.Close

Set FileOut = FSO.OpenTextFile("C:\foo\barOut.txt", 2, True, True)
FileOut.Write Replace(Text, "findThis", "useThat")
FileOut.Close

I did not expect the output file to be so different!

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Research Unicode. Its 16 bit characters - the BOM tells us that it is UTF-16 (Little Endian) (on intel processors the least significant byte is stored first). Motorola is big endian. UTF16 BE is FEFF. – Noodles Jun 07 '19 at 04:16
  • Your code opens it as ASCII not Unicode because you omitted UNICODE from OpenTextFile. Read the docs. Note in Windows everything in memory is Unicode. – Noodles Jun 07 '19 at 04:19
  • 2
    It's clearly not possible that the sample code you posted could ever have worked, even if we ignore the missing instantiation of `FSO`. The line `Set Text = FileIn.ReadAll` would've raised an error 424 "object required". Please create a [mcve], test-run *that* code to make sure it exposes the behavior you're asking about, then [edit] your question and copy/paste *that* code. Do not fabricate stuff. Do not type from memory. – Ansgar Wiechers Jun 07 '19 at 09:35

1 Answers1

3

Change the second True in your open for writing OpenTextFile statement to zero (0). The documentation says this writes ASCII.

Set FileOut = FSO.OpenTextFile("C:\foo\barOut.txt", 2, True, 0)

....

clairestreb
  • 1,243
  • 12
  • 24