You can write to or read from a text file using ASP. The following is simple example that illustrates how to create text file and write some information to it.
<html>
<title>Create txt file </title>
<body>
<%
Set fileObj=Server.CreateObject("Scripting.FileSystemObject")
set file1=fileObj.CreateTextFile("\asp\TextFile.txt")
file1.WriteLine("This is what goes to the text file that would be created")
file1.WriteLine("This is the second line of the text file")
file1.Close
set file1=nothing
set fileObj=nothing
%>
</body>
</html>
Set fileObj=Server.CreateObject("Scripting.FileSystemObject") creates the instance of the file object. set file1=fileObj.CreateTextFile("\asp\TextFile.txt") creates the actual file, TextFile.txt within the specified path. WriteLine() writes the quoted line of text then we close the file and set instance back to nothing.
Reading from a Text File
Reading from a text file is also very easy and similar to writing to it.
The following example illustrates how to read from a text file.
<html>
<body>
<%
Set fileObj=Server.CreateObject("Scripting.FileSystemObject")
Set listFile=fileObj.OpenTextFile(Server.MapPath("\cliktoprogram\asp\list.txt"), 1)
do while listFile.AtEndOfStream = false
Response.Write(listFile.ReadLine)
Response.Write("<br>")
loop
listFile.Close
Set listFile=Nothing
Set fileObj=Nothing
%>
</body>
</html>
This example displays all the lines of the text file one by one. To display all the lines at once without line breaks, simply replace
the lines starting with do while and ending with
loop to Response.Write(listFile.ReadAll).
To display the first line of the text file use,
Response.Write(listFile.ReadLine).
To skip line of a text file use,listFile.SkipLine
To skip part of line of a text use,listFile.Skip(2). This will skip 2 characters.
To write an empty line use, WriteBlankLines
Also you can assign the line to a variable like this,text=listFile.ReadLine