How to search a text file with vbscript

Written by jason candanedo
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to search a text file with vbscript
(Burke/Triolo Productions/Brand X Pictures/Getty Images)

There are times when a programmer might need to search and read a text file when using VBScript. It is possible to do this task by accessing the Windows Script Host, through VBScript. This is in addition to taking advantage of and utilising the FileSystemObject.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Create a text file using the following name and location:

    "C:\Temp\TextToRead.txt"

  2. 2

    Open and run the Notepad program. Click the "Start" button in the Windows desktop, click "All Programs," then select "Accessories" and click on "Notepad."

  3. 3

    Copy and Paste the following 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()

    Click "File" and select "Save" in Notepad and exit the program.

  4. 4

    Run the script by double-clicking on the ReadTextFile.vbs VBScript file.

    Click the "OK" button in the "Windows Script Host" prompt. You can now search and read a text file with a VBSript.

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.