Shootace
Shootace

Reputation: 21

Javascript Append to Text File

I have several JavaScript files that when run return no errors, but do not do what is coded inside them. This is one of them. Is there something else I should have installed or enabled on the PC running the .JS files?

function WriteDemo()
{
   var fso, f, r;
   var ForReading = 1, ForWriting = 2;
   fso = CreateObject("Scripting.FileSystemObject");
   f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, true);
   f.Write("Hello world!");
   f.Close();
   f = fso.OpenTextFile("c:\\testfile.txt", ForReading);
   r = f.ReadLine();
   return(r);
}

Upvotes: 1

Views: 20019

Answers (1)

Brian McBrayer
Brian McBrayer

Reputation: 163

According to the MSDN article on FileSystemObject, for JavaScript you should use

new ActiveXObject

instead of

CreateObject

(that's for VB).

function WriteDemo() 
{
 var fso, f, r;
 var ForReading = 1, ForWriting = 2;
 fso = new ActiveXObject("Scripting.FileSystemObject");
 f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, true);
 f.Write("Hello world!");
 f.Close();
 f = fso.OpenTextFile("c:\\testfile.txt", ForReading);
 r = f.ReadLine();
 return(r);
}

And, of course, don't forget to call the function. :)

WriteDemo();

Upvotes: 2

Related Questions