Reading Files

To read data from text files, use one of the following TextStream object methods:

If using the Read or ReadLine method, to skip to a particular portion of data, use the Skip or SkipLine method. The text produced by the read method is stored in a string. The string can be displayed in a control, parsed by string functions (such as Left, Right, and Mid), concatenated, and so forth.

The following example opens, writes data to, and reads from a file:

[VBScript]
Sub ReadFiles
  Dim fso, f1, ts, s
  Const ForReading = 1
  Set fso = CreateObject(“Scripting.FileSystemObject”)
  Set f1 = fso.CreateTextFile(“c:\testfile.txt”, True)
  ‘ Write a line.
  Response.Write “Writing file <br>”
  f1.WriteLine “Hello World”
  f1.WriteBlankLines(1)
  f1.Close
  ‘ Read the contents of the file.
  Response.Write “Reading file <br>”
  Set ts = fso.OpenTextFile(“c:\testfile.txt”, ForReading)
  s = ts.ReadLine
  Response.Write “File contents = ‘” & s & “’”
  ts.Close
End Sub