Javascript FileSystemObject (FSO) textstream object provides a property AtEndOfStream that returns the status of file reading pointer whether it is at the end of any file retrived in the form textstream object. FSO OpenTextFile method supports the AtEndOfStream property when file is opened with FileReading mode. AtEndOfStream property returns the Boolean result i.e. true or false based on the position of the textstream file reading pointer. If file pointer is at the end of the text stream then it returns true otherwise it returns false.
Syntax for Javascript FSO textstream AtEndOfStream property
textStreamObj.AtEndOfStream;
textStreamObj can be initialized using FileSystemObject and its OpenTextFile method that will return the textstream retrieved from the specified text file.
Example of Javascript FSO textstream object AtEndOfStream property
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// Open the text file at the specified location with write mode
var txtFile = fso.OpenTextFile("C:\\Temp\\myFolder\\file.txt", 2, false, 0);
txtFile.WriteLine("Hello this is the first line of text file.");
txtFile.WriteLine("Hello this is the second line of text file.");
txtFile.WriteLine("Hello this is the third line of text file.");
txtFile.Close();
// Open the text file at the specified location with read mode
txtFile = fso.OpenTextFile("C:\\Temp\\myFolder\\file.txt", 1, false, 0);
var text;
// read 1st character of first line of the text file
text = txtFile.Read(1);
// returns the status of AtEndOfStream property
document.write("<b>AtEndOfStream :</b> " + txtFile.AtEndOfStream);
document.write("<br />");
// while loop used to read all lines with condition AtEndOfStream!=true
// this loop will work until AtEndOfStream=false and will exit the loop when AtEndOfStream=true
while (txtFile.AtEndOfStream != true) {
text += txtFile.ReadLine();
text += "<br />";
}
document.write("<b>Strings returned :</b> " + text);
document.write("<br />");
document.write("<b>AtEndOfStream :</b> " + txtFile.AtEndOfStream);
document.write("<br />");
txtFile.Close();
fso = null;
// Output: AtEndOfStream : false
// String returned :
// Hello this is the first line of text file.
// Hello this is the second line of text file.
// Hello this is the third line of text file.
// AtEndOfStream : true
</script>
Continue to next tutorial: Javascript FSO textstream object Column property to learn how to get the column number of the current character in a textstream.