One of the most important tasks in any programming language is the ability to read and write files. The steps involved in ASP are no different than many other languages:
- Specify the location of the file
- Determine if the file exists
- Get a handle to the file
- Read the contents
- Close the file and release any resources used
File I/O in ASP can be done using the FileSystemObject component. When opening a text file you simply open it as a text stream, and it is this text stream that you use to access the contents of the file.
The FileSystemObject allows you to perform all file and folder handling operations. It can either return a file which can then be opened as a text stream, or it can return a text stream object directly.
In the following I present two different methods. The first method gets a file object and uses that to open the text stream, and the second method opens the text stream directly from the FileSystemObject.
Method 1:
<% Option Explicit
Const Filename = "/readme.txt" Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")
Dim Filepath
Filepath = Server.MapPath(Filename)
if FSO.FileExists(Filepath) Then
Dim file
set file = FSO.GetFile(Filepath)
Dim FileSize
FileSize = file.Size
Response.Write "<p><b>File: " & Filename & " (size " & FileSize &_
" bytes)</b></p><hr>"
Response.Write "<pre>"
Dim TextStream
Set TextStream = file.OpenAsTextStream(ForReading, TristateUseDefault)
Do While Not TextStream.AtEndOfStream
Dim Line
Line = TextStream.readline
Line = Line & vbCRLF
Response.write Line
Loop
Response.Write "</pre><hr>"
Set TextStream = nothing
Else
Response.Write "<h3><i><font color=red> File " & Filename &_
" does not exist</font></i></h3>"
End If
Set FSO = nothing
%>
Method 2:
<% Option Explicit
Const Filename = "/readme.txt" Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")
Dim Filepath
Filepath = Server.MapPath(Filename)
if FSO.FileExists(Filepath) Then
Set TextStream = FSO.OpenTextFile(Filepath, ForReading, False, TristateUseDefault)
Dim Contents
Contents = TextStream.ReadAll
Response.write "<pre>" & Contents & "</pre><hr>"
TextStream.Close
Set TextStream = nothing
Else
Response.Write "<h3><i><font color=red> File " & Filename &_
" does not exist</font></i></h3>"
End If
Set FSO = nothing
%>
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.