1

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);
}
Kroltan
5,1565 gold badges40 silver badges64 bronze badges
asked Aug 8, 2014 at 13:22
4
  • 3
    That's JScript, not Javascript. Commented Aug 8, 2014 at 13:23
  • That file by itself won't do anything; the function is declared but never invoked. Commented Aug 8, 2014 at 13:26
  • 'but do not do what is coded inside them'. What does it do instead? Commented Aug 8, 2014 at 13:31
  • You should consider not doing this Commented Aug 8, 2014 at 13:31

1 Answer 1

2

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();
answered Aug 8, 2014 at 13:28
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, makes sense. I am used to VBScript. I thought the ActiveXObject was for calling something in IE. Thank you Brian!
No problem. If this helped, you can mark it as the correct answer. Thanks! :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.