Click here to Skip to main content
6,636,867 members and growing! (21,272 online)
Email Password   helpLost your password?
Web Development » ASP » General     Intermediate License: The Code Project Open License (CPOL)

Reading a text file in ASP

By Chris Maunder

How to read a text file on a server using VBScript in ASP
VBScript, Windows, ASP, IIS, Visual Studio, Dev
Version:3 (See All)
Posted:20 Jan 2000
Updated:31 Oct 2001
Views:430,512
Bookmarked:38 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
55 votes for this article.
Popularity: 7.27 Rating: 4.18 out of 5
1 vote, 9.1%
1

2
1 vote, 9.1%
3
1 vote, 9.1%
4
8 votes, 72.7%
5

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)

About the Author

Chris Maunder


Member
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.
Occupation: Founder
Company: The Code Project
Location: Canada Canada

Other popular ASP articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 59 (Total in Forum: 59) (Refresh)FirstPrevNext
GeneralThanks for the helpful insights... PinmemberDestiny7772:41 16 May '09  
Questionhow to read 2nd Line PinmemberPushkar Joshi20:58 7 Mar '06  
Generalgood PinsussAnonymous3:31 25 Oct '05  
GeneralReading Text in ASP PinmemberKaKaShi2018:20 11 Jul '05  
Generalread html file from remote location Pinmemberdeshkar20:01 11 Feb '05  
GeneralRe: read html file from remote location Pinsuss[z]ULu2:43 14 Feb '05  
Generalpage processing halts when accessing text files with ASP Pinsussmarsdust21:26 18 Oct '04  
Generalread from HTM file Pinsusslisahan11:10 31 Aug '04  
GeneralRe: read from HTM file [modified] PinmemberZhelezov0:20 24 Apr '07  
GeneralReading a text file in ASP PinmemberK.CHANDRASEKARAN20:14 5 Jul '04  
GeneralRe: Reading a text file in ASP PinsussAnonymous1:25 26 Jul '04  
GeneralReading Remote Files PinsussTim Kluger7:44 14 Jun '04  
Generalhelps Pinmemberal_3asel1:43 23 May '04  
GeneralHow can i search in a text file Pinmemberemmatty1:49 11 May '04  
GeneralBinary Read PinmemberTomazZ0:21 11 Mar '04  
Generalcurly brace being converted to &#123; PinsussAnonymous3:59 12 Feb '04  
GeneralERROR (file not found) but it exists PinsussAnonymous15:21 6 Jan '04  
GeneralRe: ERROR (file not found) but it exists PinmemberChristian Graus15:46 6 Jan '04  
GeneralRe: ERROR (file not found) but it exists PinsussAnonymous3:16 14 Jan '04  
GeneralRe: ERROR (file not found) but it exists PinsussAnonymous20:21 19 Jan '04  
GeneralERROR (file not found) but it exists PinsussAnonymous15:20 6 Jan '04  
GeneralRe: ERROR (file not found) but it exists PinsussAnonymous7:01 25 Feb '05  
GeneralHELP! I cannot read from text file, my page stuck. Pinmembervast236:11 27 Jul '03  
GeneralRe: HELP! I cannot read from text file, my page stuck. Pinsusskluis16:26 1 Oct '03  
GeneralVBScript help and ideas PinsussAnonymous21:08 20 Jul '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Oct 2001
Editor: Chris Maunder
Copyright 2000 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project