How to read from a text file using VBScript?

I am looking to see an easy way to read and write to a text file using VBScript.

I think this is an acceptable method for writing to a file.

Dim f, Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.CreateTextFile("C:\test.txt", True, True) f.WriteLine("Data to Add to file.") f.Close 

However, I would like to know how to read from a file in a similar way.

+4
windows scripting vbscript
source share
1 answer

First use the OpenTextFile method, and then ...

or read the file immediately with the ReadAll method:

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

or line by line with the ReadLine method:

 Set dict = CreateObject("Scripting.Dictionary") Set file = fso.OpenTextFile ("c:\test.txt", 1) 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 
+25
source share

All Articles