Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify text file on Server 1

Status
Not open for further replies.

alokwww

Technical User
Dec 25, 2002
86
Hi,
I have very very little ASP experience. Let me tell you what I'm trying to do, and hopefully you might be able to help me out:

I have a text file named text.txt on my server. I want to create a web page that is basically just a big comment box that takes its value from the text file, and when I press the Submit button, it will submit any changes I've made within the comment box into the text file.

Is ASP capable of doing something like this?
How might I be able to do this?

Thanks!
-Alok
 
Yes ASP can do this, if you have the MS Scripting Documention search on FileSystemObject, or search the MS KB.

Let me know if you need more.

 
I was able to find the following script with html:


<%
set FSO = Server.CreateObject(&quot;scripting.FileSystemObject&quot;)
set myFile = fso.CreateTextFile(&quot;C:\Inetpub\ true)
myFile.WriteLine(&quot;howdy)
myFile.Close
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set fso = server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
set f = fso.GetFile(&quot;test.txt,true&quot;)
set ts = f.OpenAsTextStream(ForReading, -2)
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine
%>

<html>
<head>
<title>Enter Data</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;formEnter&quot; method=&quot;post&quot; action=&quot;<Server.CreateObject(&quot;scripting.FileSystemObject&quot;)>
<p> <textarea rows=5 cols=60 name=&quot;var1&quot;>
Enter text here.
</textarea>
</p>

<p>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
<input type=&quot;reset&quot; name=&quot;Submit2&quot; value=&quot;Reset&quot;>
</p>
</form>
</body>
</html>


Whenever I submit it it gives me the page cannot be displayed and shows: <Server.CreateObject( in the address bar. Anyone have any suggestions.

Thanks for your replies!

-Alok
 
Change this: action=&quot;<Server.CreateObject(&quot;scripting.FileSystemObject&quot;)> in the <Form> tag to:
action=&quot;<Server.request.servervariables(&quot;SCRIPT_NAME&quot;)>
 
I changed it to look like:


<%
set FSO = Server.CreateObject(&quot;scripting.FileSystemObject&quot;)
set myFile = fso.CreateTextFile(&quot;test.txt&quot;, true)
myFile.WriteLine(&quot;howdy)
myFile.Close
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set fso = server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
set f = fso.GetFile(&quot;test.txt,true&quot;)
set ts = f.OpenAsTextStream(ForReading, -2)
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine
%>

<html>
<head>
<title>Enter Data</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;formEnter&quot; method=&quot;post&quot; action=&quot;<Server.request.servervariables(&quot;SCRIPT_NAME&quot;)>
<p> <textarea rows=5 cols=60 name=&quot;var1&quot;>
Enter text here.
</textarea>
</p>

<p>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
<input type=&quot;reset&quot; name=&quot;Submit2&quot; value=&quot;Reset&quot;>
</p>
</form>
</body>
</html>


This still shows up in the address bar:


C:\Documents and Settings\Alok\Desktop\web2\<Server.request.servervariables(


and I still get the Page cannot be displayed stuff.

Thanks again for your replies!
 
Sorry. I was in too big a hurry and copied...
action=&quot;<%=request.servervariables(&quot;SCRIPT_NAME&quot;)%>&quot;

If you are uncomfortable with that then use the name of your .asp page ie. action=&quot;myPage.asp&quot;

It just means that the form is going to submit to itself.
 
I must be doing something wrong:
I put that in so its:


<%
set FSO = Server.CreateObject(&quot;scripting.FileSystemObject&quot;)
set myFile = fso.CreateTextFile(&quot;test.txt&quot;, true)
myFile.WriteLine(&quot;howdy)
myFile.Close
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set fso = server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
set f = fso.GetFile(&quot;test.txt,true&quot;)
set ts = f.OpenAsTextStream(ForReading, -2)
Do While not ts.AtEndOfStream
myText = myText & ts.ReadLine
%>

<html>
<head>
<title>Enter Data</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;formEnter&quot; method=&quot;post&quot; action=&quot;<%=request.servervariables(&quot;SCRIPT_NAME&quot;)%>&quot;
<p> <textarea rows=5 cols=60 name=&quot;var1&quot;>
Enter text here.
</textarea>
</p>

<p>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
<input type=&quot;reset&quot; name=&quot;Submit2&quot; value=&quot;Reset&quot;>
</p>
</form>
</body>
</html>


But I get the same errors and it still says: C:\Documents and Settings\Alok\Desktop\web2\<%=request.servervariables(

in the address bar

Thanks again for you help!
-Alok
 
You're missing the closing form tag:
<form name=&quot;formEnter&quot; method=&quot;post&quot; action=&quot;<%=request.servervariables(&quot;SCRIPT_NAME&quot;)%>&quot; >
 
Delete it and try this one. Just replace your text file name with &quot;YourFile.txt&quot; and put it in the same directory as this file (on your server) until you figure out what's going on. I know this one works because I just put it together and tried it on my server. It will also allow you to update the text file.

<%
const ForReading=1
const ForWriting=2
strTextFile=server.MapPath(&quot;YourFile.txt&quot;)
set fso = server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
%>
<form action=&quot;<%=request.ServerVariables(&quot;SCRIPT_NAME&quot;)%>&quot; method=&quot;post&quot;>
<textarea name=&quot;txtContent&quot; rows=&quot;10&quot; cols=&quot;70&quot; wrap=&quot;physical&quot;>
<%
if len(request.Form(&quot;btnUpdate&quot;)) then
NewText = request.Form(&quot;txtContent&quot;)
arrLines = split(NewText,vbcrlf)
set tStream=fso_OpenTextFile(strTextFile,ForWriting)
For x = 0 to UBound(arrLines)
ThisLine = arrLines(x)
if len(ThisLine) > 4 then
tStream.WriteLine mid(ThisLine,6)
end if
Next
tStream.close
End if

set tStream = fso.openTextFile(strTextFile,ForReading)
Do while not tStream.AtEndOfStream
LineNum=tStream.Line
strLine=Right(&quot;00&quot; & cstr(LineNum),3)
strText = tStream.Readline
response.Write(strLine & &quot;: &quot; & strText & vbcrlf)
loop
tStream.Close
%>
</textarea>
<%
if len(request.Form(&quot;btnUpdate&quot;)) then
NewText = request.Form(&quot;txtContent&quot;)
arrLines = split(NewText,vbcrlf)
set tStream=fso_OpenTextFile(strTextFile,ForWriting)
For x = 0 to UBound(arrLines)
ThisLine = arrLines(x)
if len(ThisLine) > 4 then
tStream.WriteLine mid(ThisLine,6)
end if
Next
tStream.close
End if
%>
<p><input type=&quot;submit&quot; name=&quot;btnUpdate&quot; value=&quot;Update&quot;></p>
</form>

<%
set fso=nothing
%>
 
Okay, Somethings up here. Should I be saving this as something besides .html? I tried to save it in my cgi-bin directory and even that didn't work. Do I need to do anything else special?

Take a look at what happens (this is with the code copied and pasted exactly from your previous post):



Thanks again for all of your troubles!
 
Absolutely! Save it as .asp - that's the only way it's going to work. Once the page is saved somewhere on your server, you'll have to request it by typing the url into your browser. It won't fly if you double-click.
 
BTW - This forum is Microsoft Active Server Pages (ASP), so 99% of the examples will be about or from or for .asp pages. As a rule: Anything you grab from here, name it with an .asp extension. :)
 
Awesome, I'm getting somewhere now; the text from YourFile.txt shows up in the textbox, but when I hit update, YourFile.txt doesnt get updated and I get an &quot;internal server error&quot;.


Veep, You're awesome! Thanks so much for all of your expert and quick responses!
-Alok
 
How big is the file that you're updating? I'm pretty sure that there's a limit to the size but I don't know off-hand what it is. I was using a relatively small text file (about 75 lines) and didn't have any problems at all. Just an idea...
 
From when I was testing, the files only 17 lines.

Thanks again!
-ALok
 
Hey ALok,

I know this item is a bit old now, but I am trying to do the same exact thing. I am not getting the posted code to work correctly.

Did you ever resolve this? If so, could you please post the code you used.

Thanks!
Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top