How to read from a text file using VBScript?

后端 未结 2 1354
刺人心
刺人心 2020-12-01 19:37

I am looking to see a simple way to read from and write to a text file using VBScript.

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

 D         


        
2条回答
  •  抹茶落季
    2020-12-01 19:56

    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 = 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
    

提交回复
热议问题