Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Friday, June 23, 2006

read text file via jscript

Here are two code snippets to read text file, it seems that FileSystemObject
is convenient than ADODB.Stream when the file is not stored in Unicode.
function LoadLogMsg(input_filename)
{// read a text file and return a string
var Stream = new ActiveXObject("ADODB.Stream")
//Stream.Mode = 3;// read write
Stream.Type = 2;// adTypeText
Stream.Charset = "iso-8859-1"; //default is unicode
Stream.Open();
Stream.LoadFromFile(input_filename);
//Stream.Position = 0;
var log_msg = Stream.ReadText(-1);//adReadAll
Stream.Close();
WScript.Echo (log_msg);
return log_msg;
}

function LoadLogMsg2(input_filename)
{//read a text file and return a string
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFile(input_filename);
var a = f.OpenAsTextStream( 1); //forreading
var log_msg = a.ReadAll();
a.Close();
return log_msg;
}

0 Comments:

Post a Comment

<< Home