locked
Windows 10 JScript Text File Open/Close -- File not really closed! RRS feed

  • Question

  • This is a flaw in Win-10. Explanation follows.

    I write a lot of programs in JScript. (This has nothing to do with web development, I'm using JScript as a scripting language.)

    I have a certain function I use a lot, called "log( )". You give it a string, and it copies the string to the command window, and to a log file. It's simple. In outline, it looks like this:

    function log( message )
    {    WScript.Echo( message );
         LL = fso.OpenTextFile( LOGFILE, 8, true); //8=appending, true=create if needed
         LL.WriteLine( message );
         LL.Close();
    }

    I left out a few details for clarity, like some error checking, and fso = new ActiveXObject("Scripting.FileSystemObject"); To avoid any chance of collision, this function creates it's own fso, every time you call it.

    When I started to use this function on Win-10, it FAILED ALL THE TIME! How can it fail, you ask? It's been working fine on Win-EverythingElse for the last 12+ years.

    Well, for starters, I very often use it like this:

        log("Incremental  = " + Incremental  );

        log("Invariant      = "  + Invariant       );

        log("CycleBased   = " + CycleBased   );

        log("Traditional    = " + Traditional    );


    That is to say, one rapid-fire log( ) statement after another. So it's trying to open, close, and then re-open the same file in rapid succession.

    It was failing on LL = fso.OpenTextFile( ). So my hypothesis was, "What if the LL.Close( ) method doesn't really close the file? What if it's still open when you call OpenTextFile( ) the next time?"

    Sure enough, I fixed it like this. Again, I've left out some details for clarity.

    function log(message)
    {    WScript.Echo( message );

         success = false;

         while( !success ){
             try {
                 LL = fso.OpenTextFile( LOGFILE, 8, true); //8=appending, true=create if needed

                 success = true;
             } //try
             catch(e){
                 success = false;   //and do something sensible
                 WScript.Sleep(1);
             } //catch(e)

         } //while( !success )

         LL.WriteLine( message );

         LL.Close();
        
    } //function log( )

    Via this and various other tests, I was able to convince myself that my hypothesis was correct. LL.Close( ) doesn't really close the file.

    I think this is a flaw in the operating system. When you call a close( ) function, the file should be well and truly closed before it gives control back to the caller.

    What sayest thou?

    Sunday, March 24, 2019 5:25 PM

All replies

  • Do not keep opening the file.  Open once and leave open.  Windows will prevent issues.


    \_(ツ)_/

    Sunday, March 24, 2019 5:41 PM
  • Even if you do keep re-opening the file this would be the correct way to do it.

    function log(message){
        WScript.Echo( message );
        try {
            LL = fso.OpenTextFile( LOGFILE, 8, true); //8=appending, true=create if needed
     	LL.WriteLine( message );
            LL.Close();
        }
        catch(e){
        	WScript.Echo e.Description
        }
        
    } //function log( )
    

    If this fails then you have other issues and need to capture the exception reason. 

    In WSH script you cannot easily share files with other processes.  If you use the stream write and open the file as shared from both processes then this can be done.


    \_(ツ)_/

    Sunday, March 24, 2019 6:37 PM