views:

132

answers:

1

Hello,

I am writing a destop script on windows 2003 and I need to open a file and seek to the end of it and read the last line. I looked for a "seek" but couldn't find. I saw the openTextFile for option but didn't have.

I implement it by openning the file with the red flag and then reading line after line. With big file it takes a time,

Do any one know how to do this quickly (either in vb script or javascript)

+1  A: 

I can't think of a straightforward way to do it except for maybe reading the whole file, splitting into an array and popping the last line off:

var fso   = new ActiveXObject("Scripting.FileSystemObject");
var tf    = fso.OpenTextFile("c:\\testfile.txt", 1 /*for reading*/, false);

// Split all lines into an array
var lines = tf.ReadAll().split("\r\n");

// Get the last line from the file:
var lastLine = lines.pop();

I've done this on a number of occasions and it should be faster than a loop.

Andy E
Yes, this is the best way doing that, 10x for the help
Roman Dorevich