windows 如何使用 VBScript 从文本文件中读取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/854975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read from a text file using VBScript?
提问by Curtis Inderwiesche
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.
但是,我想知道如何以类似的方式读取文件。
回答by Jonas Elfstr?m
Use first the method OpenTextFile
, and then...
首先使用方法OpenTextFile
,然后...
either read the file at once with the method ReadAll
:
使用以下方法立即读取文件ReadAll
:
Set file = fso.OpenTextFile("C:\test.txt", 1)
content = file.ReadAll
or line by line with the method ReadLine
:
或逐行使用方法ReadLine
:
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