locked
How do you read Unicode file with JScript? RRS feed

  • Question

  • This has probably been asked before but I can't find the answer. I can't seem to read a Unicode file. When I run the following against a file saved as Unicode, the first line is 3 strange characters followed by a lot of blank lines. If I save the file as ANSI and run it again every line is displayed with the few Unicode chars displayed as "?".

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var WshShell = WScript.CreateObject("WScript.Shell");
    var sr = fso.OpenTextFile("C:\\foo\\unifile.txt", 1, -1);
    while (!sr.AtEndOfStream)
    {
        WScript.Echo(sr.ReadLine());
    }

    Displaying the chars isn't really the problem. I need to be able to work with the text read. This was just a simple example of the issue.

    Thanks.

    Sunday, May 4, 2008 5:06 PM

Answers

  • oops I miss a trick there.  Your OpenTextFile is missing a parameter.  I suspect the -1 on the end is meant to indicate that the file should be read as unicode.  However the third  parameter of the OpenTextFile method is Create which indicates whether the file should be created if it doesn't already exist.  You should be passing false as this parameter and passing -1 as the forth Format parameter.

    Monday, May 5, 2008 10:41 AM

All replies

  •  

    First thing to do is open the file with notepad.  Confirm it contains what you think it should.  Then use save as... in notepad and confirm the encoding being used is actually Unicode.

     

     

     

     

    Sunday, May 4, 2008 9:20 PM
  • Created the file using Notepad and saved it as Unicode.
    Sunday, May 4, 2008 9:53 PM
  • oops I miss a trick there.  Your OpenTextFile is missing a parameter.  I suspect the -1 on the end is meant to indicate that the file should be read as unicode.  However the third  parameter of the OpenTextFile method is Create which indicates whether the file should be created if it doesn't already exist.  You should be passing false as this parameter and passing -1 as the forth Format parameter.

    Monday, May 5, 2008 10:41 AM
  • Thanks. Looks like that was the issue.
    Monday, May 5, 2008 1:24 PM