How to Read a CSV File in ASP

Written by david a. wells
  • Share
  • Tweet
  • Share
  • Email

Active Server Pages (ASP) is a server side scripting language produced by Microsoft. It offers a wide range of programming functions that allow Web programmers to capture, store and manipulate data through a website. One of the common ways to store information is with a simple text file saved on the server. Data records are commonly stored in text files using the Comma Separated Values (CSV) format. Each line in the text file represents a different record, which is composed of several fields of data separated by commas.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Create an instance of the file system object. The file system object is a standard object within ASP that allows for the manipulation of files on the server.

    <%

    Set fso=Server.CreateObject("Scripting.FileSystemObject")

    %>

  2. 2

    Open the text file with the OpenTextFile function. This function requires the file name parameter and a mode parameter that tells the script if the file is open for reading or writing. In this example the mode parameter is set to "1" or read only.

    <%

    Set fso=Server.CreateObject("Scripting.FileSystemObject")

    Set file=fso.OpenTextFile(Server.MapPath("CSVFile.txt"), 1)

    %>

  3. 3

    Read the contents of the first line in the CSV file. Each line within the CSV file will contain several pieces of data separated by commas. In this example the entire first line is written to the Web page with the ReadLine function. Alternatively, you could use the ReadAll function, instead of ReadLine, which would print the contents of the entire file to the Web page.

    <%

    Set fso=Server.CreateObject("Scripting.FileSystemObject")

    Set file=fso.OpenTextFile(Server.MapPath("CSVFile.txt"), 1)

    Response.Write(file.ReadLine)

    %>

  4. 4

    Close the file system object and destroy it once the data has been retrieved from the CSV file.

    <%

    Set fso=Server.CreateObject("Scripting.FileSystemObject")

    Set file=fso.OpenTextFile(Server.MapPath("CSVFile.txt"), 1)

    Response.Write(file.ReadLine)

    file.Close

    Set file=Nothing

    Set fso=Nothing

    %>

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.