Read line per line a txt file with VBS

后端 未结 2 1745
离开以前
离开以前 2020-12-16 19:57

I\'m trying this code:

filename = \"test.txt\"
listFile  = fso.OpenTextFile(filename).ReadAll
listLines = Split(listFile, vbCrLf)
For Each line In listLines
         


        
2条回答
  •  不思量自难忘°
    2020-12-16 20:17

    Your code seems to be working fine. I changed it slightly to show that the lines are, in fact, read line-by-line:

    Set fso=CreateObject("Scripting.FileSystemObject")
    
    filename = "test.txt"
    listFile = fso.OpenTextFile(filename).ReadAll
    listLines = Split(listFile, vbCrLf)
    i = 0
    
    For Each line In listLines
       WScript.Echo CStr(i) + " : " + line 
       i = i + 1
    
       'My Stuff
    Next
    

    I assume fso is set in your script somewhere, but I added that extra line just for completeness.

    You should verify that your input file does, indeed, have multiple lines separated by vbCrLf. The counter i helps in debugging each line as it shows the line index during reading the lines.

提交回复
热议问题