eHow launches Android app: Get the best of eHow on the go.

How To

How to Read the Contents of a Text File Using VBScript

Member
By who10
User-Submitted Article
(1 Ratings)
Information Technology
Information Technology
John C. Hooten

At times, when programming with VBScript, a programmer will need to read the contents of a text file.
This can be accomplished by accessing the Windows Script Host via VBScript and making use of the FileSystemObject.

The program below will walk you through the steps necessary to accomplish this task and give you a real world example to learn from.

Difficulty: Moderately Easy
Instructions

Things You'll Need:

  • Computer
  • Windows(98 or newer)
  1. Step 1

    Create a Text File

    Create a text file with the following name at the following location.
    C:\Temp\TextToRead.txt

    Within the text file, add the following lines.
    If you get this message,
    you have successfully
    completed this
    programming task.

  2. Step 2

    Open Notepad

    Go to Start - All Programs - Accessories - Notepad
    Save your file as C:\Temp\ReadTextFile.vbs

  3. Step 3

    Copy Code

    Copy and Paste the following code into Notepad.

    '*****************************************************************
    Option Explicit

    Const conForReading = 1

    'Declare variables
    Dim objFSO, objReadFile, contents

    'Set Objects
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objReadFile = objFSO.OpenTextFile("C:\Temp\TextToRead.txt", 1, False)

    'Read file contents
    contents = objReadFile.ReadAll

    'Close file
    objReadFile.close

    'Display results
    wscript.echo contents

    'Cleanup objects
    Set objFSO = Nothing
    Set objReadFile = Nothing

    'Quit script
    WScript.Quit()
    '******************************************************************
    Save your file and exit Notepad

  4. Step 4
    Success!
     
    Success!

    Run Script

    Double click on your ReadTextFile.vbs VBScript file.
    You should get the window shown.
    Select "OK"


    Congratulations! You've written a VBScript capable of reading a text file.

Tips & Warnings
  • When viewing or editing VBScript, make sure you right-click on the file and choose Edit, otherwise you will run the script
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics