Read and write into a file using VBScript

前端 未结 9 1507
轮回少年
轮回少年 2020-11-27 06:34

How can we read and write some string into a text file using VBScript? I mean I have a text file which is already present so when I use this code below:-

Set         


        
9条回答
  •  情书的邮戳
    2020-11-27 07:16

    This is for create a text file

    For i = 1 to 10
        createFile( i )
    Next
    
    Public Sub createFile(a)
    
        Dim fso,MyFile
        filePath = "C:\file_name" & a & ".txt"
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set MyFile = fso.CreateTextFile(filePath)
        MyFile.WriteLine("This is a separate file")
        MyFile.close
    
    End Sub
    

    And this for read a text file

    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set dict = CreateObject("Scripting.Dictionary")
    Set file = fso.OpenTextFile ("test.txt", 1)
    row = 0
    Do Until file.AtEndOfStream
      line = file.Readline
      dict.Add row, line
      row = row + 1
    Loop
    
    file.Close
    
    For Each line in dict.Items
      WScript.Echo line
      WScript.Sleep 1000
    Next
    

提交回复
热议问题