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
:
Set file = fso.OpenTextFile("C:\test.txt", 1)content = file.ReadAll
or line by line with the method ReadLine
:
Set dict = CreateObject("Scripting.Dictionary")Set file = fso.OpenTextFile ("c:\test.txt", 1)row = 0Do Until file.AtEndOfStream line = file.Readline dict.Add row, line row = row + 1Loopfile.Close'Loop over itFor Each line in dict.Items WScript.Echo lineNext
Dim obj : Set obj = CreateObject("Scripting.FileSystemObject")Dim outFile : Set outFile = obj.CreateTextFile("in.txt")Dim inFile: Set inFile = obj.OpenTextFile("out.txt")' Read fileDim strRetVal : strRetVal = inFile.ReadAllinFile.Close' Write fileoutFile.write (strRetVal)outFile.Close