End of file in JavaScript

12,784

Solution 1

Try:

f.AtEndOfStream

http://msdn.microsoft.com/en-us/library/ffk3x3bw%28v=vs.84%29.aspx

Solution 2

You're getting back a FileStream object, so you want to use one of the properties that returns boolean to do what you're trying to do, such as AtEndOfStream

However, now I'm going to get out my soapbox and ask why you're writing stuff for windows scripting with javascript, and why you don't choose to use something more modern like PowerShell, which will give you most of the flexibility of .NET objects, but almost all the scriptability of BASH or other scripting languages.

Then the method you're trying to accomplish here would be much easier to write/work with/get support on from other community members.

We're highly unlikely to recall the esoteric specifics of windows jscript programming (but obviously there are a few of us old farts still around)

Share:
12,784
xiaoke
Author by

xiaoke

Updated on June 14, 2022

Comments

  • xiaoke
    xiaoke about 2 years

    I want to read a local file line by line in JavaScript. Ut I can check if it goes to the end of this file. For example:

    file = new ActiveXObject("Scripting.FileSystemObject"); 
    
    // Sets the variable "f" to equal the new open file contents 
    
    f = file.OpenTextFile("c:\\wantlist.txt", 1); 
    s = f.ReadLine()
    
    While(s != EOF)
    {
        //. Do some thing 
        s = f.ReadLine()
    }
    

    What is EOF should be in JavaScript? Or do you have another way to deal with this problem? Thank you very much!

  • jcolebrand
    jcolebrand over 11 years
    Just out of curiousity, did you have a reason to choose version .84 over my choice of .60? It doesn't really matter, but I thought I would be nosy for a moment. It's obvious OP is writing WSH files, and so will likely have to support back as far as possible. Ergo giving the eldest likely records from the APIs works best, since there's fairly little forward-breakage on scripting languages.
  • The Alpha
    The Alpha over 11 years
    May be you should make a complete working answer because he's using s = f.ReadLine(); inside while but I think it should be s += f.ReadLine();
  • Sheridan Bulger
    Sheridan Bulger over 11 years
    I really just chose the first result for AtEndOfStream on Google to get the MSDN page. I didn't really put a whole lot of thought into what version I chose, because such a simple property ports pretty well. I figured once he knew the name of the property, he could choose the API version suiting his liking while on MSDN already, although the sample code is probably the same regardless.
  • jcolebrand
    jcolebrand over 11 years
    Why would I write a complete example? I wanted to give him the best answer that specifically gave what he wanted. Additionally, just to be clear, what you're asking remarks that he might be trying to read in all the lines and keep them all in a buffer. Traditionally you read each line one-by-one so you can operate on each line. An example: A CSV file, where each line is one record. Therefore keeping two or more lines in memory would be duplicates of a buffer. I think OP was fairly specific on what the usage was.