Search code examples
filevbscriptcodepages

How can I stop my vbscript from writing a different type of file than the one I read?


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!


Solution

  • 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)
    

    ....