I am looking to see a simple way to read from and write to a text file using VBScript.
我希望看到一种使用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 fashion.
但是,我想知道如何以类似的方式从文件中读取。
1 个解决方案
#1
Set file = fso.OpenTextFile("C:\test.txt", 1)
content = file.ReadAll
or line by line
或逐行
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
#1
Set file = fso.OpenTextFile("C:\test.txt", 1)
content = file.ReadAll
or line by line
或逐行
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