VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

"TextStream" Objects Representing File Input and Output

This section describes TextStream object and its properties and methods. A tutorial example is provided on how to open a new file, write some text lines, and read them back.

TextStream: An object representing an input or output stream connected to a file. It offers the following properties and methods:

  • "AtEndOfLine": Property to return true if the current reading position is before the end of a line.
  • "AtEndOfStream": Property to return true if the current reading position is before the end of the file.
  • "Column": Property to return the column number of the current position.
  • "Line": Property to return the line number of the current position.
  • "Close()":
  • "Read(n)": Method to return a string of characters up to the specified number.
  • "ReadAll()": Method to return a string of the rest characters in the stream.
  • "ReadLine()": Method to return a string of the rest characters in the linie.
  • "Skip(n)":
  • "SkipLine()":
  • "Write(string)":
  • "WriteLine([string])":
  • "WriteBlankLines(n))":

Here is a VBScript example to show you how to create and open a text file.

<html>
<body>
<!-- text_stream_test.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   ' Getting the FileSystemObject
   set fs = CreateObject("Scripting.FileSystemObject")

   ' Creating a new file for writing
   set out = fs.CreateTextFile("c:\temp\test.txt",true,false)
   out.WriteLine("FirstName=Bill")
   out.WriteLine("LastName=Smith")
   out.WriteLine("Email=bill@com.com")
   out.Close()

   ' Open an existing file for reading
   set inn = fs.OpenTextFile("c:\temp\test.txt",1)
   while inn.AtEndOfStream = false
      line = inn.ReadLine()
      document.writeln(line)
   wend
   inn.Close()
</script>
</pre>
</body>
</html>

When you load this example into IE, you will get a security warning. You can ignore it and get the output:

Tests on TextStream class:
FirstName=Bill
LastName=Smith
Email=bill@com.com

Note that:

  • If you look at the directory c:\temp, you will see a new file "test.txt".
  • "in" seems to be a reserved word. So I used "inn" as a variable name.
  • "wend" ends the "while" statement. I really don't know why the designers can not use "end while".

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

scrrun.dll - Scripting Runtime DLL Library

 Scripting Runtime DLL Library Overview

 "Dictionary" Objects to Store Keys and Values

 "FileSystemObject" Objects to Manage File Systems

 "Drive" Objects Representing Disk Drives

 "Folder" Objects Representing File Folders

 "File" Objects Representing Files

"TextStream" Objects Representing File Input and Output

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

"TextStream" Objects Representing File Input and Output - Updated in 2015, by Dr. Herong Yang