How to read from a text file using VBScript?

Use first the method OpenTextFile, and then…

either read the file at once with the method ReadAll:

Const ForReading = 1
Dim file, content

Set file = fso.OpenTextFile("C:\test.txt", ForReading)
content = file.ReadAll

or line by line with the method ReadLine:

Const ForReading = 1
Dim dict, file, row, line

Set dict = CreateObject("Scripting.Dictionary")
Set file = fso.OpenTextFile ("c:\test.txt", ForReading)
row = 0
Do Until file.AtEndOfStream
  line = file.Readline
  dict.Add row, line
  row = row + 1
Loop

file.Close

'Loop over it
For Each line in dict.Items
  WScript.Echo line
Next

Leave a Comment