Link to home
Start Free TrialLog in
Avatar of igfp
igfp

asked on

count number of lines in a file

hi everyone, is it possible to count the number of lines in a file?
I'm using:

    Dim strTemp As String * 1
    Dim i As Integer
    i = 0
    Open App.Path & "\aras.pol" For Input As 1#
    Do Until EOF(1)
    i% = i% + 1
    Line Input #1, strTemp
    Loop
    Close #1

msgbox "number of lines: " & i

this works but is there another way?
Avatar of igfp
igfp

ASKER

oh, and also to go to a specific line. I'm using the same thing but when i= "number of the line I want" then it exits the cycle. Is there another way to do this as well?
ASKER CERTIFIED SOLUTION
Avatar of stefri
stefri
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Similar to stefri and yours, you can read the entire file in one shot, then "count" the number of newline characters.

dim strTemp as strnig
dim strLinesSplit() as string
dim iNumLines as integer

' read the entire file
Open App.Path & "\aras.pol" For Input As 1#
strTemp = input$(lof(1), #1)
Close #1

' count the number of newlines, then add one for the last line
strLinesSplit = Split(strTemp, vbNewLine)
iNumLines = UBound(linesSplit) - 1

--
The key here is the Input$ and the Split functions.
Avatar of igfp

ASKER

my question now is, since i like mine the best, wich one takes the points????
>is there another way
Since this Q was answered in the first comment (stefri), I think you should accept that as your answer.
Avatar of igfp

ASKER

as you wish! ;) Thank you both!