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);
}
-
3That's JScript, not Javascript.Kroltan– Kroltan2014年08月08日 13:23:14 +00:00Commented Aug 8, 2014 at 13:23
-
That file by itself won't do anything; the function is declared but never invoked.Pointy– Pointy2014年08月08日 13:26:32 +00:00Commented Aug 8, 2014 at 13:26
-
'but do not do what is coded inside them'. What does it do instead?The Archetypal Paul– The Archetypal Paul2014年08月08日 13:31:13 +00:00Commented Aug 8, 2014 at 13:31
-
You should consider not doing thisvol7ron– vol7ron2014年08月08日 13:31:29 +00:00Commented Aug 8, 2014 at 13:31
1 Answer 1
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
Brian McBrayer
1632 silver badges8 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Shootace
Ok, makes sense. I am used to VBScript. I thought the ActiveXObject was for calling something in IE. Thank you Brian!
Brian McBrayer
No problem. If this helped, you can mark it as the correct answer. Thanks! :)