Page 229 - DCAP408_WEB_PROGRAMMING
P. 229
Unit 9: ASP Objects
Open a text file for reading Notes
This example opens the file “Textfile.txt” for reading.
ASP CODE OUTPUT
<html> Hello World line 1
<body> Hello World line 2
Hello World line 3
<%
Set FS =
Server.CreateObject("Scripting.FileSystemObject")
Set RS = FS.OpenTextFile(Server.MapPath("text") &
"\TextFile.txt",1)
While not rs.AtEndOfStream
Response. Write RS.ReadLine
Response. Write("<br />")
Wend
%>
<p>
<a href="text/textfile.txt"><img border="0"
src="/images/btn_view_text.gif"></a>
</p>
</body>
</html>
Homemade hit counter
This example reads a number from a file, adds 1 to the number, and writes the number back to
the file.
ASP CODE OUTPUT
<% This page has been visited
Set 12345 times.
FS=Server.CreateObject("Scripting.FileSystemObject")
Set
RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1,
False)
fcount=RS.ReadLine
RS.Close
fcount=fcount+1
'This code is disabled due to the write access
security on our server:
'Set
RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2,
False)
'RS.Write fcount
'RS.Close
Set RS=Nothing
Set FS=Nothing
%>
<html>
<body>
<p>
This page has been visited <%=fcount%> times.
</p>
</body>
</html>
LOVELY PROFESSIONAL UNIVERSITY 223