SoFunction
Updated on 2025-04-13

How to read the contents of a text file?

Ever want to know how to display the contents of a text document using ASP. Here is a easy way to read
from a text file

<!--Start of ASP Code---->

<%
'by James Seymour,

Dim write
Dim fileSysObj, tf, read

' Read the

' Store the file name where the Information is stored into a variable called read

read = ""

' Retrieve the fullpath of the read file

read = LEFT((("PATH_INFO")), InStrRev(
(("PATH_INFO")), "\")) & read

' Create an instance of FileSystem Object and store it into a variable called fileSysObj

Set fileSysObj = createObject("")

' Check whether the read file exists

IF ((read)) Then
' if the file exists, then open it for reading
Set tf = (read, 1)
read =

ELSE
' if you can't find , display default message
read = "I can't find the file ! So this is my default message."

END IF
%>


' table the displays the file
<div align="center">
<center>
<table border="0" width="40%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" bgcolor="#EEEECC"><B><%=read%></B>
</td>
</tr>
<tr>
<td width="100%">


<!-- End of the ASP -->


use the code above, click and drag your mouse over the code to highlight it. Then right click on the
highlighted code and click "Copy." Now you may paste it into your code editor.