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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update txt file on web server

Status
Not open for further replies.

ktwclark

Programmer
Jan 30, 2002
54
GB
Hi

I'm looking to have an incremental counter (not a hit counter) as a simple text file within my web folder. Is it possible to open this file, read the number it contains, add one to it, then save the file again. This number then becomes the reference number of another document.

Does the file need special permissions set up to allow anyone to read/write to the file?
 
Did you ever figure this out? I am new to VBScript and am trying to do a similar thing by saving to a database.
 
I've gotten this far -


<SCRIPT language=VBScript>
<!--
Sub ReadWriteText_OnClick()
Dim fso, f1, ts, s

'Open file and read contents
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set ts = fso_OpenTextFile(&quot;C:\testfile.txt&quot;, 1)
s = ts.ReadLine
msgbox &quot;File contents = '&quot; & s & &quot;'&quot;
ts.Close

'Open file for writing
Set f1 = fso_OpenTextFile(&quot;c:\testfile.txt&quot;, 2, True)
s = s + 1
f1.WriteLine s
f1.WriteBlankLines(1)
f1.Close
msgbox &quot;File contents = '&quot; & s & &quot;'&quot;
End Sub
-->
</SCRIPT>

I can work off C:, or specify a drive letter and path, but cannot get it ot work using a URL, i.e.
Any ideas?
 
Problem Solved, Isn't the Internet great!

<%
Dim fso, f1, ts, contents, server_path
'find server path to file asp.asp (filename), therefore take of 7 chars to get web server path
server_path = mid(Server.MapPath(Request.ServerVariables(&quot;path_info&quot;)),1,len(Server.MapPath(Request.ServerVariables(&quot;path_info&quot;))) -7)
Response.Write server_path
'Open file and read contents
Set fso = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set ts = fso_OpenTextFile(server_path & &quot;testfile.txt&quot;, 1)
contents = ts.ReadLine
response.Write &quot;File contents = '&quot; & Contents & &quot;'&quot;
ts.Close
'Open file for writing
Set f1 = fso_OpenTextFile(Server_Path & &quot;testfile.txt&quot;, 2, True)
contents = contents + 1
f1.WriteLine contents
f1.WriteBlankLines(1)
f1.Close
Response.write &quot;File contents = '&quot; & contents & &quot;'&quot;
%>
 
Sorry, should have mentioned that I am now using ASP to do the work and the page that contains the above code is called asp.asp and is sitting in the root of the web server. So, when I look to get the path of the file the code is currently running in, I need to take off the filename to arrive at the physical server name and root.

My example used as the virtual path, using the code returns E:\Drive1\asp.asp so to get the physical path I remove 7 characters (asp.asp) to arrive at the physical path.

All figured out on my own (with the help of a few online ASP tutorials)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top