0

Can any one please tell me that we use ReadLine() to read a particular line from a file (.txt). Now I want to read the total content of the file (not only the first line). For that what method I need to use. I googled a lot but I cant get the solution.

My Code is given below:

    var ForReading = 1;
    var TristateUseDefault = -2;
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var newFile = fso.OpenTextFile(sFileName, ForReading, true, TristateUseDefault);
    var importTXT = newFile.ReadLine();

This is returning the first line of the .txt file by importTXT variable. Now I want to get the total file content in importTXT.

Any suggestion will be very much helpful for me.

Arindam Rudra
  • 592
  • 2
  • 8
  • 22

2 Answers2

1

Here: ReadAll (msdn)

I found the example given very poor - for example it did not CLOSE the file, so I added this to the msdn page:

function ReadAllTextFile(filename)
{
    var ForReading = 1;
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    // Open the file for input.
    var f = fso.OpenTextFile(filename, ForReading);

    // Read from the file.
    var text = (f.AtEndOfStream)?"":f.ReadAll(); // this is where it is read
    f.Close();
    return text;
}
var importTXT = ReadAllTextFile(sFileName);
mplungjan
  • 134,906
  • 25
  • 152
  • 209
1

You use the ReadAll method:

var importTXT = newFile.ReadAll();

(Don't forget to close the stream when you are done with it.)

Guffa
  • 640,220
  • 96
  • 678
  • 956