windows 如何使用VBScript读取文本文件?

rdrgkggo  于 2023-11-21  发布在  Windows
关注(0)|答案(2)|浏览(166)

我期待看到一个简单的方法来读取和写入一个文本文件使用VBScript。
我认为这是一种可以接受的写入文件的方法。

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

字符串
但是,我想知道如何以类似的方式从文件中读取。

vsikbqxv

vsikbqxv1#

首先使用方法OpenTextFile,然后...
或者使用方法ReadAll立即读取文件:

Const ForReading = 1
Dim file, content
Dim fso

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

字符串
或者使用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

f45qwnt8

f45qwnt82#

Dim obj : Set obj = CreateObject("Scripting.FileSystemObject")
Dim outFile : Set outFile = obj.CreateTextFile("in.txt")
Dim inFile: Set inFile = obj.OpenTextFile("out.txt")

' Read file
Dim strRetVal : strRetVal = inFile.ReadAll
inFile.Close

' Write file
outFile.write (strRetVal)
outFile.Close

字符串

相关问题