3.0 beta 2

writing to a text file using javascript

How can I write to a text file using Javascript?

I want to retrieve information from the user and write it to a text file. Can anyone help me in this regard?
Ramesh
August 19,
No one can, JavaScript is not allowed to write anything to the hard drive. There are no read/write methods for the local hard drive, sorry.
Jim Hunter
August 19,
If the amount of text you want to store is not too big, you could use cookies.
Josef Stich
August 19,
Here you are... Only on Windows though...

function WriteToFile(sText){

var fso = new ActiveXObject("Scripting.FileSystemObject");
varFileObject = fso.OpenTextFile("C:\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
newObject.write(sText)

newFileObject.close()
}
Barkis
October 21,
not work of the coding :
function WriteToFile(sText){

var fso = new ActiveXObject("Scripting.FileSystemObject");
varFileObject = fso.OpenTextFile("C:\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
newObject.write(sText)

newFileObject.close()
}

I tried :

function WriteToFile(sText){

var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile("C:\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
new Object.write(sText)

new FileObject.close()
}

or

new FileObject.write(sText)

also not work .. anything missing?
conny
October 27,
Check in IE to make sure that Active Scripting is enabled (Security tab/Custom Level). The above code will not work in any browser other then IE and I think it only works in IE 6.0+.
Jim Hunter
October 27,
my IE is active scripting enable and version 6.0+

here I found can work script :

function WriteToFile(sText){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\test.txt", true);
s.WriteLine("helloworld");
s.Close();
}
conny
October 31,
The script won't work from a internet source since the internet is not trusted to modify file on your machine

If this is a internal web app then make the site trusted that should work but it's a long time since I played with this my self hope this helps
October 31,
<html><head>
<script language="javascript">
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");

var s = fso.CreateTextFile("C:\\LogFile.txt", true);

<%String s1="Hai";%>

s.WriteLine('<%=s1%>');
s.WriteLine('***********************');
s.WriteLine('<%=s1%>');
s.WriteLine('<%=s1%>');

s.Close();
}
</script>
</head>
<body onLoad="WriteToFile()">

</body>
</html>
N.Srinivas Vamsi Krishna
November 25,
This works:

<span onClick="file()">Hello</span></p>

<SCRIPT LANGUAGE="JavaScript">
function file()
{
var ForAppending = 8;
var TriStateFalse = 0;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newFile = fso.OpenTextFile("c:\\WINDOWS\\Desktop\\001122.html",
ForWriting, true, TriStateFalse);
}
</SCRIPT>
Dmin
April 11,
No code is working properly..vat to do?
Might i tried out all net sources..
Hi-Man
April 27,
USE XML... Read about it first from the internet then try it.
ali
September 1,
This script won't work in a nomal web browser. Try saving the script as a hta and running it on a local machine. It will work if your secutity settings are correct, my security on Windows XP is too high. I have however made whole applications on Windows 2000 with this kind of script. Cheers.
Justin
September 17,
Javascript doesnt allow u to modify contents on ur hard disk from an external web page!!!!
October 3,
Javascript doesnt allow u to modify contents on ur hard disk from an external web page!!!!If it does it will be hackers dream come true!!
AP
October 3,
How to create file in folder in that is page?
Borna
October 22,
Javascript does allow you to modify contents on your hard-disk. That is why a lot of browsers do not allow ActiveXObject - hackers could easily inject crap into your computer. IE 6.0 allows it on certain settings.
December 24,
IE 6.0 allows it on certain settings:

There is no problem to run EXE files or change file system from JS ,
but you MUST allow IE to do so.
First at all you have to enable some options in IE (default hidden).

There is ready to use VBS script to show this options,
copy it to new file: showMyComputer.vbs ,
then run the script on your machine.
set shell = createobject("wscript.shell")
'=====
'Changing this under the HKLM makes it effective for all users
'at next logon...
'=====
HKLM_MyComputer_key = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\0\"
valuename = "Flags"
shell.RegWrite HKLM_MyComputer_key & valuename, 1, "REG_DWORD"
'=====
'Changing this under the HKCU makes it effective for this user
'immediately and at every logon...
'=====
HKCU_MyComputer_key = "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\0\"
valuename = "Flags"
shell.RegWrite HKCU_MyComputer_key & valuename, 1, "REG_DWORD"

msgbox "The 'My Computer' security zone is now visible in the IE security dialog."

wscript.quit


Now enter to IE security preferences and change values at the end of list to enabled.

From this moment tricks like this work !! ( enter in IE Adress ):

file:\\c:\windows\system32\cmd.exe

Also anything called from JS:
window.open('file:\\...', ... )

will run local files.

It works on my XP.
Ceasar
December 30,
function WriteToFile(sText){

var fso = new ActiveXObject("Scripting.FileSystemObject");
varFileObject = fso.OpenTextFile("C:\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
newObject.write(sText)

newFileObject.close()
}

Code above has some syntax problems!! check the underlines code.

Corrected code is as below: this does works!! I have checked it...

function WriteToFile(sText)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile("C:\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
FileObject.write(sText)
FileObject.close()
}

Njoy!
Vinayak
vinayak.kadam@techbooks.com
Vinayak Kadam
February 14,
var fso = new ActiveXObject("Scripting.FileSystemObject");
when we use this object I always get the following error in
my browser "An activex contral .........blah blah........usafe to run"
for God sake can anyone tell me is there any workaround for this????????? btw I use IE6
WeShall
February 16,
RE: "for God sake can anyone tell me is there any workaround for this????????? btw I use IE6"

might help if you also read the comments?

try the one with the nice colors ;) tells you everything you want to know...
JustAnotherNoob
April 3,
<html><head>
<script language="javascript">
function Writedata()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var write_id;
write_id = document.getElementById('write_id').value ;
alert('The data has been written to \n' + write_id);
var s = fso.CreateTextFile(write_id, true);

s.WriteLine(document.getElementById('name_id').value);
s.WriteLine(document.getElementById('id_id').value);

s.Close();
}
</script>
</head>
<body>
name : <input type="text" name="name" value="" id="name_id"><br/>
Address : <input type="text" name="id" value="" id="id_id"><br/>
Write file to : <input type="text" name="write" value="" id="write_id"><br/>
<input type="button" onclick="Writedata()" value="submit">
</body>
</html>

this is a workable page and can be tried out for hands on feel ..... but do not forget to enable the ActiveX scripting from tools->internet options->security tab->custom level
soumya sen
June 22,
I have seen it done in javascript, but I only know how to in vbscript (the code is almost identical)
Here is The VBScript Code:
Sub SaveText
strText = "I made this text file in VBScript"

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFileName = "C:/Test_File_save/test.txt"
Set objFile = objFSO.CreateTextFile(strFileName)
objFile.Write strText
objFile.Close
End Sub

You would save this to an hta file and set it so that the Sub is called when the body loads

Aditionally, this code is a full hta, which saves a text file to C:/Documents and Settings/notes. To Change it, find strFileName, and change the path that is in the Quotes. This will allow a user to save the file to a place on the hard drive with their own contents and file name! I have tested all the code and it works in an hta:
<html>
<head>
<title>Test</title>
<HTA:APPLICATION
ID="objTest"
APPLICATIONNAME="My Application Name"
SCROLL="yes"
SINGLEINSTANCE="no"
CONTEXTMENU="no"
ICON="include path to favicon here"/>


</head>

<SCRIPT LANGUAGE="VBScript">
Sub SaveText
strText = DataArea.Value

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFileName = "C:/Documents and Settings/notes/" & SaveName.Value
Set objFile = objFSO.CreateTextFile(strFileName)
objFile.Write strText
objFile.Close
End Sub


</SCRIPT>
<body>

<textarea name="DataArea" rows=5 cols=70></textarea><p>
Enter File Name For Note: <input type="text" name="SaveName" value="*.txt">
<input id=runbutton type="button" value="Save" name="run_button" onClick="SaveText">
<p>
<p><BR>
<P>
</body>
</html>

Anonymus
August 20,
hello people,

sorry, my englash is bad!

I have a form and I like take the answers this form and salve .txt in javascript or JSP. Who I do that?
Diego
October 22,
This is an interesting thread btw. I started laughing when I came to its end. :D
November 21,
Thanks, the code is really good.......
Ujjwal Soni
December 29,
thanks for these codes!
February 22,
Thanks to everyone here. I was playing with my perfect script for a whole day on IE7 with Vista and it was really frustrating because I could double click on the ".js" file and it would create a text file but NO way would the same script work inside the browser. I tried every conceivable security setting, code tweak, zone. Nothin' worked.

Then I put in the few lines mentioned above into my <HEAD> to turn the little devil into a "HTA" (which I'd never heard of before this thread) and low and behold it works like a charm.

Just for the geeks among you, I've now got a Flash app which reads in a file, screws around with it, and then passes the output to a Javascript program (in a HTA of course) which writes the manipulated code into a file on the hard drive. Sweeeet. :-) btw.. the Flash app is also embedded in the HTA code.


Shaun Bliss, Blissweb
March 5,
Hi people, I've been trying to use these codes, but they don't actually write to the file. The code can create the file but it is blank. My form needs to have about 20 different fields (It's an order form for internal use) unfortunately it isn't working with even a single field.

Can anyone explain why the code might not write to the file?
Niki
March 28,
Never mind, I got it to work.

Originally, I was using document.getElementById to pull up the form's fields however, this resulted in text document which just said "[object]" for each ID. I'm assuming that it couldn't transform the input into text before trying to write it, but the code below works perfectly.

I can not use this code to write to the C: (I blame Vista), but that is an insignificant problem - just write it to a subdirectory instead.

<script language='JavaScript'>
function WriteToFile() {
 try {
   var fso, s;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   s = fso.OpenTextFile("C:\\Test\\Logfile.txt" , 8, true);
   s.writeline("name|email|location|comments");
   s.writeline(document.data.name.value + "|" +  document.data.email.value + "|" + document.data.location.value + "|" +  document.data.comments.value);
   s.Close();
 }
catch(err){
  var strErr = 'Error:';
  strErr += '\nNumber:' + err.number;
  strErr += '\nDescription:' + err.description;
  document.write(strErr);
 }
}


</script>
<body>

<form action="test.hta" method="post" name="data">
Name:     <input type = "text" name="name"><br>
Email: <input type="text" name="email"><br>
Location: <input type = "text" name="location"><br>
Comments: <textarea name="comments"> </textarea><br>
<input type=button value="Submit Data" OnClick="WriteToFile(this.form)">
</form>


I have used | as a delimiter so that the file can then be reliably used in a mail merge.
Niki
April 1,
I use Vista but can't get the code above from Niki to work. I am trying to use this code in an authoring tool (Lectora) but can't get it to work

SPecifically, I get Error:NumberUndefined and Error:DescriptionUndefined.

Thanks.
April 20,
I used Soumya Sen's script and it works great in IE7, but I get no response from firefox. If anyone has a FF workable version in Java please mention.

Thanks though great script
August 29,
"WriteToFile(this.form)">
October 2,

This topic is archived.

See also:


Back to support forum

Search

Version 2

Looking for AW 2.6 ?
The old site is here.