Read line per line a txt file with VBS
Asked Answered
U

2

10

I'm trying this code:

filename = "test.txt"
listFile  = fso.OpenTextFile(filename).ReadAll
listLines = Split(listFile, vbCrLf)
For Each line In listLines
   WScript.Echo line 
   'My Stuff
Next

Or this other:

filename = "test.txt"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(filename, ForReading)
    Do Until f.AtEndOfStream
      myLine = f.ReadLine
      WScript.Echo myLine
      'My Stuff
    Loop

Why in both cases it echoes all lines at once, and of course I'm unable to work line by line? Any idea?

Unutterable answered 12/5, 2014 at 22:13 Comment(0)
M
12

Your file has funny EndOfLine markers. Let's assume the lines are terminated by vbLf:

>> fn = "lf.txt"
>> goFS.CreateTextFile(fn).Write Replace("a b c ", " ", vbLf)
>> set ts = goFS.OpenTextFile(fn)
>> do until ts.AtEndOfStream
>>    WScript.Echo ts.ReadLine
>> loop
>>
a
b
c

As you can see, .ReadLine can cope with vbLf (unix). Your Split() on .ReadAll(), however, will fail:

>> t = goFS.OpenTextFile(fn).ReadAll()
>> a = Split(t, vbCrLf)
>> WScript.Echo UBound(a)
>> WScript.Echo a(0)
>>
0
a
b
c

t does not contain a single vbCrLf, so Split() returns an array with UBound() == 0, containing t as its single element. .Echoing that will at least look like 3 (4) lines. You could Split() on vbLf, if you really need an array of lines.

But if your files contains vbLf endings, then the .ReadLine loop should work fine.

.ReadLine() can't cope with vbCr (mac):

>> fn = "cr.txt"
>> goFS.CreateTextFile(fn).Write Replace("a b c ", " ", vbCr)
>>
>> set ts = goFS.OpenTextFile(fn)
>> do until ts.AtEndOfStream
>>    WScript.Echo ts.ReadLine
>> loop
>>
c

The b+cr 'overwrites' the a+cr and is then 'overwritten' by c+cr. The .ReadAll() approach will fail too, unless you use vbCr as separator.

But if your files contains vbCr endings, then none of your snippets can "echoe(s) all lines at once".

Does your file come from outer space?

Update wrt comment:

You can't read UTF-8 using the Filesystemobject. Either convert the file to UTF-16 and use the Unicode option of the format parameter when .OpenTextFile it, or work with an ADODB Stream.

It still would be interesting to know what EOL marker is used.

Mayor answered 12/5, 2014 at 23:0 Comment(4)
+1 for the outer space question. (I wonder why nobody asked for a sample file in the question comments?)Pottage
my file comes from notepad++ around 20.000 lines, in ANSI (tested with utf8 without bom) Under windows 7 64Unutterable
Well I think the problem comes from the source file, I'm creating it with another script that appends a new line using myFile.WriteLine( line.Item(0)) m I doing it wrong?Unutterable
perfect finally the problem was caused by EndOfLine markers, thanks!Unutterable
R
3

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.

Roussel answered 12/5, 2014 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.