Reading TXT Files in VBScript

A .txt file is a document that contains non-formatted text viewable in any text-editing program. VBScript is an active scripting language designed by Microsoft based on Visual Basic. VBScript can be used for Web client and Web server scripting. You may have an instance where you will need to read a .txt file using VBScript. With the Windows Notepad text editor and using the "FileTextObject," you can read the .txt file in VBScript.

Instructions

    • 1

      Click "Start," "All Programs," "Accessories" and "Notepad" to open the text editor.

    • 2

      Click "File," then "New" to open a new file in Notepad.

    • 3

      Type the following script:

      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()

    • 4

      Replace "C:\Temp\TextToRead.txt" with the directory location of the txt file you want to read in VBScript. Click "File" and "Save" to save the file.

    • 5

      Double-click the "ReadTextFile.vbs" file to read the txt file in VBScript and click "OK."

Related Searches:

References

Resources

Comments

Related Ads

Featured