VbScript - Read File

How to read a txt file using the VbScript programming ?   

1.      Create a Text File
      Create a text file with the following name at the following location.

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

   2.      Open Notepad
      Go to Start - All Programs - Accessories - Notepad
      Save your file as C:\TestRead.vbs   ' A VbScript file

   3.            Copy and Paste the following code into any Text Editor.

      Option Explicit

      Const conForReading = 1

      'Declare variables
      Dim objFSO, objReadFile, contents

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

      'Read file contents
      contents = objReadFile.ReadAll

      'Close file

      'Display results
      wscript.echo contents

      'Cleanup objects
      Set objFSO = Nothing
      Set objReadFile = Nothing

      'Quit script
      Save your file and exit Notepad

4. Execute the vbs file either from cmd prompt or by double clicking.

Post a Comment


Close Menu