Click here to Skip to main content
12,767,468 members (47,030 online)
Click here to Skip to main content
Add your own
alternative version

Stats

661.7K views
3.8K downloads
51 bookmarked
Posted 20 Jan 2000

Reading a text file in ASP

, 18 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
How to read a text file on a server using VBScript in ASP

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:

  1. Specify the location of the file
  2. Determine if the file exists
  3. Get a handle to the file
  4. Read the contents
  5. 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"    ' file to read
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

' Create a filesystem object
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")

' Map the logical path to the physical system path
Dim Filepath
Filepath = Server.MapPath(Filename)

if FSO.FileExists(Filepath) Then

    ' Get a handle to the file
    Dim file    
    set file = FSO.GetFile(Filepath)

    ' Get some info about the file
    Dim FileSize
    FileSize = file.Size

    Response.Write "<p><b>File: " & Filename & " (size " & FileSize  &_
                   " bytes)</b></p><hr>"
    Response.Write "<pre>"

    ' Open the file
    Dim TextStream
    Set TextStream = file.OpenAsTextStream(ForReading, TristateUseDefault)

    ' Read the file line by line
    Do While Not TextStream.AtEndOfStream
        Dim Line
        Line = TextStream.readline
    
        ' Do something with "Line"
        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"    ' file to read
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

' Create a filesystem object
Dim FSO
set FSO = server.createObject("Scripting.FileSystemObject")

' Map the logical path to the physical system path
Dim Filepath
Filepath = Server.MapPath(Filename)

if FSO.FileExists(Filepath) Then

    Set TextStream = FSO.OpenTextFile(Filepath, ForReading, False, TristateUseDefault)

    ' Read file in one hit
    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
%>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionThis cat must be in tip-top shape! Pin
B. Clay Shannon21-Jan-14 13:58
professionalB. Clay Shannon21-Jan-14 13:58 
AnswerRe: This cat must be in tip-top shape! Pin
Chris Maunder21-Jan-14 14:19
adminChris Maunder21-Jan-14 14:19 
GeneralRe: This cat must be in tip-top shape! Pin
B. Clay Shannon21-Jan-14 14:34
professionalB. Clay Shannon21-Jan-14 14:34 
GeneralRe: This cat must be in tip-top shape! Pin
Chris Maunder21-Jan-14 14:35
adminChris Maunder21-Jan-14 14:35 
GeneralRe: This cat must be in tip-top shape! Pin
B. Clay Shannon21-Jan-14 14:36
professionalB. Clay Shannon21-Jan-14 14:36 
GeneralRe: This cat must be in tip-top shape! Pin
Chris Maunder21-Jan-14 15:55
adminChris Maunder21-Jan-14 15:55 
Questionreading text file from java Pin
goofy7827024-Sep-12 9:04
membergoofy7827024-Sep-12 9:04 
GeneralMy vote of 5 Pin
Chimeco17-Oct-11 5:56
memberChimeco17-Oct-11 5:56 
GeneralThanks for the helpful insights... Pin
Destiny77716-May-09 2:41
memberDestiny77716-May-09 2:41 
Questionhow to read 2nd Line Pin
Pushkar Joshi7-Mar-06 20:58
memberPushkar Joshi7-Mar-06 20:58 
Generalgood Pin
Anonymous25-Oct-05 3:31
sussAnonymous25-Oct-05 3:31 
GeneralReading Text in ASP Pin
KaKaShi2011-Jul-05 18:20
memberKaKaShi2011-Jul-05 18:20 
Generalread html file from remote location Pin
deshkar11-Feb-05 20:01
memberdeshkar11-Feb-05 20:01 
GeneralRe: read html file from remote location Pin
[z]ULu14-Feb-05 2:43
suss[z]ULu14-Feb-05 2:43 
Generalpage processing halts when accessing text files with ASP Pin
marsdust18-Oct-04 21:26
sussmarsdust18-Oct-04 21:26 
Generalread from HTM file Pin
lisahan31-Aug-04 11:10
susslisahan31-Aug-04 11:10 
GeneralRe: read from HTM file [modified] Pin
Zhelezov24-Apr-07 0:20
memberZhelezov24-Apr-07 0:20 
GeneralReading a text file in ASP Pin
K.CHANDRASEKARAN5-Jul-04 20:14
memberK.CHANDRASEKARAN5-Jul-04 20:14 
GeneralRe: Reading a text file in ASP Pin
Anonymous26-Jul-04 1:25
sussAnonymous26-Jul-04 1:25 
GeneralReading Remote Files Pin
Tim Kluger14-Jun-04 7:44
sussTim Kluger14-Jun-04 7:44 
Generalhelps Pin
al_3asel23-May-04 1:43
memberal_3asel23-May-04 1:43 
QuestionHow can i search in a text file Pin
emmatty11-May-04 1:49
memberemmatty11-May-04 1:49 
GeneralBinary Read Pin
TomazZ11-Mar-04 0:21
memberTomazZ11-Mar-04 0:21 
Generalcurly brace being converted to &amp;#123; Pin
Anonymous12-Feb-04 3:59
sussAnonymous12-Feb-04 3:59 
GeneralERROR (file not found) but it exists Pin
Anonymous6-Jan-04 15:21
sussAnonymous6-Jan-04 15:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 18 Mar 2009
Article Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid