CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 1999
    Location
    Bangalore
    Posts
    60

    Counting the records in a file

    Hi all
    Can anybody give me an idea how to cnt the number or records/lines in a txtfile without reading the file.

    Thanks in advance
    Venky


  2. #2
    Join Date
    Jan 2000
    Posts
    21

    Re: Counting the records in a file

    I don't think so. You would have to open the file
    and set up a count varible in a For Next Loop to
    count the recrods and then close the file.


  3. #3
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Counting the records in a file

    You would have to Open the File but here's a Quick way to Count Records if you have VB6, using the Replace Function, if you don't have VB6, you can write a Replace Equivelant Function:
    private Sub Command1_Click()
    Caption = CountRecords("C:\TheFile.txt")
    End Sub

    Function CountRecords(byval FileName as string, optional byval Delimiter as string = vbCrLf) as Long
    Dim iFile as Integer
    Dim lFileLen as Long
    Dim lChunk as Long
    Dim sChunk as string
    Dim sData as string

    'Open the File and Load it into a string Variable..
    iFile = FreeFile
    Open FileName for binary Access Read as iFile
    lFileLen = LOF(iFile)
    lChunk = 64000
    While Loc(iFile) < lFileLen
    If Loc(iFile) + lChunk > lFileLen then lChunk = lFileLen - Loc(iFile)
    sChunk = Space(lChunk)
    get #iFile, , sChunk
    sData = sData & sChunk
    Wend
    Close iFile
    'Quickly Retrieve the No. Of Records by Removing all Record Delimiters and
    'Comparing the Length of the File Before and After..
    CountRecords = (lFileLen - len(Replace(sData, Delimiter, ""))) / len(Delimiter)

    End Function



    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  4. #4
    Join Date
    Dec 1999
    Location
    Bangalore
    Posts
    60

    Re: Counting the records in a file


    Hi Aron, Jhonpc

    Thanks for ur replies.
    I tried these methods but it is very slow as I have to read 1000 files with each file containing more than 1000 records. If u know any other method please reply back.

    Thanks
    Venky


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured