PDA

View Full Version : [SOLVED:] EOF question



remy988
11-19-2014, 07:37 PM
i need to count the number of rows of data in two different 2gb text file. this is my code

ct = 0
Open File2Open For Input As #1
Do While Not EOF(1)
Line Input #1, InputData
ct = ct + 1
Loop
Close #1


this works for the first file however for the 2nd file I get a count of only 84,474 rows, which is wrong. So I did an import starting at row 84,473; I was able to import 900000 rows of data. I didn't see anything out of the ordinary with the data.

is there a better way to write my code?

thanks
rem

pike
11-20-2014, 12:03 AM
Hi remy988 (http://www.vbaexpress.com/forum/member.php?44480-remy988),
can you adapt the code below?

Dim fso As Object, txtData
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtData = fso.OpenTextFile(yourfullFilePathandName,8, True)
MsgBox "This text file has " & txtData.Line & " line[s]."
txtData.Close
Set fso = Nothing

remy988
11-20-2014, 08:45 AM
pike,
works like a charm. i was able to figure out how to read each line as well.
thanks!

snb
11-20-2014, 10:24 AM
3 alternatives:


Sub M_snb_001()
c00 = CreateObject("Scripting.FileSystemObject").OpenTextFile("G:\OF\uitdraai 6a.txt").ReadAll
MsgBox "This text file has " & (Len(c00) - Len(Replace(c00, vbCrLf, ""))) / 2 & " line[s]."
End Sub


Sub M_snb_002()
Open "G:\OF\uitdraai 6a.txt" For Input As #1
c00 = Input(LOF(1), 1)
Close

MsgBox "This text file has " & (Len(c00) - Len(Replace(c00, vbCrLf, ""))) / 2 & " line[s]."
End Sub



Sub M_snb_003()
With CreateObject("ADODB.Recordset")
.Open "SELECT * FROM uitdraai6a.txt", "Driver={Microsoft Text Driver (*.txt; *.csv)};" & "Dbq=G:\OF\", 3
MsgBox "This text file has " & .RecordCount & " line[s]."
End With
End Sub

remy988
11-21-2014, 08:37 AM
hi SNB,
i was not successful with the 3 alternatives
run-time error 7
run-time error 5
run-time error -2147467259

one solution is good enough for me :-)

can you or pike please explain why my original code failed?

snb
11-21-2014, 08:50 AM
Of course you will have to adapt the path and the filename to your own situation....

remy988
11-21-2014, 10:27 AM
snb,
yes, i did correct the filepath & name.

snb
11-21-2014, 04:27 PM
Please show your code.
Indicate which line errors out.